
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2017 02:27 AM
Hi,
I have a urgent requirement to be finished and it is as follows.
I have a date field on catalog form and i Need to restrict user from selecting less than 5 working days date from current date and also need to restrict user from selecting weekends.
I have gone through few links but it is pretty confusing. please help me on this.
Thanks
Regards,
Musab
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2017 06:26 AM
Hi,
I have completed this requirement successfully. below code will work for me.
CS:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var cdt = newValue; //The date field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
g_form.hideFieldMsg('due_date');
var day = new GlideAjax('ClientDateTimeUtils');
day.addParam('sysparm_name','getDateDifferenceExcWeekends');
day.addParam('sysparm_fdt', cdt);
day.addParam('sysparm_difftype', dttype);
day.getXML(checkDay);
function checkDay(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
// alert(answer);
if(answer == 'Sunday' || answer == 'Saturday'){
g_form.showFieldMsg('due_date', "Date should not be on Weekends " , 'error');
// alert("Date should not be in Weekend");
g_form.clearValue('due_date'); // Pass the variable name here
g_form.setMandatory('due_date');
}
else
{
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
if(answer<0 || answer<5){
g_form.showFieldMsg('due_date', "Date should be more than 5 working days from today " , 'error');
//alert('Change value');
// alert("Date should be more than 5 working days from today.");
g_form.clearValue('due_date'); // Pass the variable name here
g_form.setMandatory('due_date');
}
}
}
}
Script Include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Takes a Single Date/Time Field and returns its time difference from nowDateTime().
//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
getNowDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
//Private function to calculate the date difference return result in second, minute, hour, day.
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
},
getDateDifferenceExcWeekends: function(){
//Make Sure that start and end are GlideDateTime Objects
var firstDT = this.getParameter('sysparm_fdt');
var gdt = new GlideDateTime(firstDT);
var days = gdt.getDayOfWeekLocalTime();
//var days = firstDT.getGlideObject().getDayOfWeek();
gs.log('Days is: '+days);
var day;
if (days == 1) {
day = 'Monday';
} else if (days == 2) {
day = 'Tuesday';
} else if (days == 3) {
day = 'Wednesday';
} else if (days == 4) {
day = 'Thursday';
} else if (days == 5) {
day = 'Friday';
} else if (days == 6) {
day = 'Saturday';
} else {
day = "Sunday";
}
return day;
}
});
P.S : I got this code from community and modified as per my requirement.
Please hit like or helpful if it has helped.
Thanks.
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 10:33 PM
hi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2020 01:43 PM
I'm trying to restrict the weekend day(I have a date field type) but the format is not correct. View that the day is not correct:
If you can help me I appreciate!!
Thanks!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2020 09:29 AM
Hi Julian,
That was the working code I have posted , can you check code there in my correct answer and try if that helps.?
Please hit helpful if my code helps
Regards,
Musab
Regards,
Musab