- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2020 12:05 AM
Hi,
How can we calculate business days between two dates i.e. one date value selected in Start Date Field and the other date would be the current Date(Today Date). I have tried the below code but it does not gives me the correct business days value:
gs.info(gs.now());
var date1 = '10/06/2020';
var dc = new DurationCalculator();
dc.setSchedule('Sys Id of the Schedule');
var dur = dc.calcScheduleDuration(date1,gs.now())/3600;
gs.info('Duration is' + dur);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2020 04:39 AM
Add date in correct format
var date1 = '2020-06-10';
var end = new GlideDateTime(date1);
And try again ...
Mark my ANSWER as CORRECT and HELPFUL if it helps...
Refrence:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2020 12:11 AM
Go through the below link thoroughly you will get your answer , check response of DVP
Mark my ANSWER as CORRECT and HELPFUL if it helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2020 03:26 AM
Hi Creativethinker/Ankur
Have tried as per the links provided in the post, but the final duration gives me weird results always as 93 days. Below is my script:
var start = new GlideDateTime(gs.now());
gs.info('Start Date' + start);
var date1 = '10/06/2020';
var end = new GlideDateTime(date1);
gs.info('End Date' +end);
var dc = new DurationCalculator();
dc.setSchedule('Sys ID');
var dur = dc.calcScheduleDuration(start,end);
var business_days = dur/(60*60*8);
gs.info('Duration is' + business_days);
var gdt = new GlideDateTime(end);
var day_of_month = gdt.getDayOfWeekUTC();
if(day_of_month < 6)
business_days += 1;
gs.info('Duration is' + business_days);
Please correct me where I have doing wrong. Motive is to get the difference from the date selected by the user against the Today Date.
Result:
*** Script: Start Date2020-05-27 00:00:00
*** Script: End Date2020-10-06 00:00:00
*** Script: Duration is93.125
*** Script: Duration is94.125
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2020 04:39 AM
Add date in correct format
var date1 = '2020-06-10';
var end = new GlideDateTime(date1);
And try again ...
Mark my ANSWER as CORRECT and HELPFUL if it helps...
Refrence:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2020 05:54 AM
Hi,
I believe the output is correct
If you see the total difference in days between 28th May and 6th October -> 131 days
Now from 27th may to 6th October how many weekend days are there -> 37
So 131-37 = 94
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader