Calculate only Working Days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2018 08:10 PM
I want to calculate date difference between 2 dates excluding weekends.
var current_date = new GlideDate();
var created_date = current.getValue('opened_at');
For example : if created date is 05-jan-2018 and current_date is 08-jan-2018 the date difference should be 2 days instead it is showing 4days which includes weekends.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2018 08:39 PM
You could do it with code like below
var st = new GlideDateTime(current.opened_at);
var ed = new GlideDateTime(gs.nowDateTime());
var days = 0 ;
while(st.getValue() < ed.getValue){
if(st.getDayOfWeek != 6 && st.getDayOfWeek != 7){ //exclude weekends
days++;
}
st.addDays(1) ;
}
gs.print(days) ;
//Logic above is not tested and may require additional changes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2020 11:08 AM
Hello,
This checks only for working days excluding weekends (glideAjax call)
I would like to share my code. Running on onchange script on catalog item. It's getting value from date catalog variable and passing to server where it's comparing to actuall date, using glideAjax. If there is picked date with more then 8 working days excluding weekends it's show fieldsmg about it.
Client - side (onchage script)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue){
GlideUI.get().clearOutputMessages();
var ga = new GlideAjax('JMLCheckDate'); //Name of the Script Include
ga.addParam('sysparm_name', 'checkDateDifference'); //Name of the function in the script include
var startDateStr = g_form.getValue('start_date'); // Get value from date field
var currentDateNum = getDateFromFormat(startDateStr, g_user_date_format); // Format date to Unix time stamp
var dtString = formatDateString(new Date(currentDateNum), g_user_date_format); // Use our function time have dd-mm-yyyy
var sDate = dtString.split('-');
var finalDate = sDate[2]+'-'+sDate[1]+'-'+sDate[0]; // Change order of date formating for backend yyyy-mm-dd
ga.addParam('sysparm_dateStart',finalDate); //Formatted date to pass to the script include
ga.getXML(DateAnswer);
}
function DateAnswer(response){ // More then 8 days show this msg
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true'){
g_form.showFieldMsg('start_date','The start date is too short to guarantee all IT provisioning before the Joiner starts');
}
}
function formatDateString(dt, format) { // Formating time stamp
var d = dt.getDate();
var M = dt.getMonth() + 1;
var yyyy = dt.getFullYear();
var dd = ('0' + d).slice(-2);
var MM = ('0' + M).slice(-2);
var yy = ('0' + yyyy).slice(-2);
var returnValue = format.replace('yyyy', yyyy);
returnValue = returnValue.replace('yy', yy);
returnValue = returnValue.replace('MM', MM);
returnValue = returnValue.replace('M', M);
returnValue = returnValue.replace('dd', dd);
returnValue = returnValue.replace('d', d);
return returnValue.toString();
}
//Type appropriate comment here, and begin script below
}
Server - side (script include)
var JMLCheckDate = Class.create();
JMLCheckDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDateDifference : function() {
var start = this.getParameter('sysparm_dateStart'); //Passing the start date from the client
var week_start = new GlideDate();
week_start.setValue(week_start.getLocalDate()); // Get date now
var week_end = new GlideDate();
week_end.setValue(start); // Set date value from client
var days = 0;
// Check if date now do not start on weekend
if (week_start.getDayOfWeekLocalTime() == 6) {
week_start.addDays(2);
} else if (week_start.getDayOfWeekLocalTime() == 7){
week_start.addDays(1);
}
while (week_start <= week_end ) { // Loop till we reach our picked date
if (week_start.getDayOfWeekLocalTime() != 6 && week_start.getDayOfWeekLocalTime() != 7) //excluding Weekend
{
days++ ; // count days
}
week_start.addDays(1);
}
if (days < 9) {
return true;
} else {
return false;
}
},
type: 'JMLCheckDate'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2020 11:27 AM
You should use schedule to calculate difference in dates.
Check below blog for solutions.
https://community.servicenow.com/community?id=community_blog&sys_id=467c62e1dbd0dbc01dcaf3231f9619ad
Regards,
Sachin