How to calculate the difference between two dates (current date and start date) in business days?

pvn
Tera Contributor

Hi All,

 

I need to calculate difference between start date of an employee and today's date in business days using a schedule defined. What is the simplest way to achieve this?

 

Thanks in advance.

5 REPLIES 5

Community Alums
Not applicable

Hi @pvn ,

try this script

 

var d1 = new GlideDateTime(current.getValue('start_date'));
var d2 = new GlideDateTime();
var dur = new GlideDuration();
dur = GlideDateTime.subtract(d1, d2); //the difference between 
var day = dur.getDayPart();

 

Hi @Community Alums 

Thanks for your response. I tried your script, but it seems it is not giving difference in business days (excluding weekends)

Manmohan K
Tera Sage

Hi @pvn 

 

You can use below script  ( Modify to add employee start date in first line )

var startDateTime = new GlideDateTime("2023-06-22 09:00:00");  //enter employee start date here

// Instantiate a new GlideDateTime object which has the current date and time
var endDateTime = new GlideDateTime(); 

var dur = new DurationCalculator();

// Set 9-5 weekday schedule. This is the schedule used below with 8 business hours

dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //enter your desired schedule sys_id

dur.calcScheduleDuration(startDateTime, endDateTime);

var secs = dur.getSeconds(); //Business time in secs

var totalSecs = dur.getTotalSeconds();  //Total time in secs

gs.print("***SCHEDULE DURATION: Business Days=" + Math.round(secs/(3600*8)) + " TOTAL Days=" + Math.round(totalSecs/(3600*24)) );

  

pvn
Tera Contributor

Hi @Manmohan K 

Thanks for your response. I tried your script and it is working, however I think it is calculating 2 days extra or may be it is timezone issue?
For eg: I have taken Start date of employee as 2023-8-15 and today's date as 2023-7-5, so business days should be 30 days if we consider 5th July as well? But the script is returning 32 days?
Note: I am not considering time in this case, comparison is between dates only.