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,

get the numeric value from that time and set it in duration field

Regards
Ankur

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

Hi Ankur,

As per below script , I am getting the duration in days. It is in Before BR

 var start = new GlideDateTime(current.approval_set); //approved time
    var end = new GlideDateTime(current.closed_at); //closed time
  
    var dc = new DurationCalculator();
    dc.setSchedule('f4db82011b36a010cb721f42b24bcb27'); //Business Hours 8 AM - 5 PM (Mon to Fri)
    var time = dc.calcScheduleDuration(start, end); 

    var days = time / (60 * 60 * 24); //converting into days
    days = days.toFixed(2); //Rounding off to 2 decimals
    current.setValue('u_fulfilment_duration', days);

 

Ex:10.5 days

But I want the answer something to be displayed as 10 days 5 hours 15 minutes 20 seconds

How can I get  answer in this format. Please guide

Hi Ankur,

I was able to get it with below 

   var start = new GlideDateTime(current.approval_set); //approved time
   var end = new GlideDateTime(current.closed_at); //closed time
    var dc = new DurationCalculator();
    dc.setSchedule('f4db82011b36a010cb721f42b24bcb27'); //Business Hours 8 AM - 5 PM (Mon to Fri)
    var time = dc.calcScheduleDuration(start, end); // time would be in seconds
   var durationMS = time*1000;
   var result = new GlideDuration(durationMS);
    var duration = result.getDisplayValue();
    current.setValue('u_fulfilment_duration', duration);

Now it is coming in days hours min seconds format

Hi Ankur,

I was able to get it with below 

   var start = new GlideDateTime(current.approval_set); //approved time
   var end = new GlideDateTime(current.closed_at); //closed time
    var dc = new DurationCalculator();
    dc.setSchedule('f4db82011b36a010cb721f42b24bcb27'); //Business Hours 8 AM - 5 PM (Mon to Fri)
    var time = dc.calcScheduleDuration(start, end); // time would be in seconds
   var durationMS = time*1000;
   var result = new GlideDuration(durationMS);
    var duration = result.getDisplayValue();
    current.setValue('u_fulfilment_duration', duration);

Now it is coming in days hours min seconds format

Hi @Ankur Bawiskar ,

Need your assistance here.

As, per above If you take the start date as 15th June, 2022 and End Date as 23rd July, 2022. SO, it comes as 28 working days 

So we are calculating as 9 hours per day and dividing it by 9 to get the business days

So,we got 10.5 business days

 

How can we get it as 28 days 5 hours 20 mins 50 seconds format. The same format is appearing in Incident duration field as well, 

But there the schedule is not used.

Please help me how can I achieve this way?