Calculate business days duration between 2 date fields

Priyanka145
Tera Contributor

Hi,

How to Calculate business days duration between 2 date fields

1 ACCEPTED SOLUTION

Hi,

907200 is seconds and it comes to 10.5 days.

Please verify it from your side what does that schedule contains, holidays etc

8 to 5 means 9 hours -> and it doesn't mean 1 day

1 day -> 24 hours

Also check the docs

Using DurationCalculator to calculate a due date

DurationCalculator calcScheduleDuration Mystery

If you take the start date as 15th June, 2022 and End Date as 23rd July, 2022

1) it comes to 28 working days excluding weekends; I am not sure if there are holidays in your schedule, I checked 8 -5 excluding holidays in my instance and there is no US holiday in it

2) so it comes to 28(days)*9(hours per day) = 252 hours

3) and 9hrs = 1 business day

4) so it comes to 252/9 = 10.5 business days

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

19 REPLIES 19

Hi @Ankur Bawiskar ,

As I have used String data type to store the duration(difference between 2 date fields), I am getting the value in the format of 1 day 2 hours 3 minutes 4 seconds.

But for reporting purpose, we need it in numeric format . Could you please guide me it is possible ?

I have even tried by creating a new field of duration type and try to set, but I am unable to set the value to duration type field.

This script was in the BR or scheduled job?

Where did we wrote this script and what was the type of fields getting populated?

 

Thanks

Saurav11
Kilo Patron
Kilo Patron

Hello Priyanka,

If you want to calculate difference between two dates and also have schedule to follow you can use the below, it will return the value in milleseconds.

var dc = new DurationCalculator();
dc.setSchedule('sysidoftheschedule');
var time = dc.calcScheduleDuration(date1,date2);

Please mark answer correct/helpful based on Impact.

Hi Saurav,

As I am checking for the difference of 2 dates excluding holidays, I have written below script, where in I got something like below

    var start= new GlideDateTime(date1);
     var end= new GlideDateTime(date2);
   
    var dc= new DurationCalculator();
    dc.setSchedule('f4db82011b36a010cb721f42b24bcb27');
     var time = dc.calcScheduleDuration(start,end);
   gs.log(time);

In the log, I got 907200(milliseconds), but where in my date1 is 15-06-2022 07:54:37 and date2 is 24-07-2022 14:02:37. Considering there is no holiday in between this and my schedule is 8 am - 5 pm excluding holidays, it should be 27 days right? How to convert to number of days and also let me know if I am missing anything?

Hello,

As Ankur pointed out the 907200 is seconds and is basically the business days which refers to the only the time taken between the schedule defined and the day will be calculated accordingly.

Thanks.