How to calculate duration in days using schedule

sowmyaj
Giga Expert

Hi Everyone,

I need to calculate the difference between two dates using "Project Management Schedule" and get the result in days.

Below is the script I used in business rule :

 

var sched = new GlideSchedule(current.schedule);
var start_date = current.u_onboard_date.getGlideObject();
var end_date = current.closed_at.getGlideObject();

var dur= sched.duration(new GlideDateTime(start_date), new GlideDateTime(end_date));

var sch_hrs=dur.getNumericValue()/3600000;  // I get the duration in hours but I want it in days
var sch_days = sch_hrs/8; // to get it in days I have hardcoded the hours assuming 8 working days per day which should be automated as there might be different scenarios for different schedule.

 

also, I tried 

var sched = new GlideSchedule('7aa3e10c8f70010040f82ab2f0f9234d');
var start='2019-07-25';
var end = '2019-07-29';

var dur= (sched.duration(new GlideDateTime(start), new GlideDateTime(end)).getDurationValue());
gs.print(dur);

 

but I want the results in days not in hours.

 

How can I achieve this?

 

Regards,

Sowmya

10 REPLIES 10

Harsh Vardhan
Giga Patron

if you are getting result in hours then you can do some math here

 

example calculate hours to days

 

var sched = new GlideSchedule('7aa3e10c8f70010040f82ab2f0f9234d'); 
var start='2019-07-25';
var end = '2019-07-29';

var dur= (sched.duration(new GlideDateTime(start), new GlideDateTime(end)).getDurationValue());
gs.print(parseInt(dur/24));

 

Note: if you are getting miliseconds then convert it to hours and then convert it to days. 

 

if getting result in miliseconds then below line will convert it to hours and then days. 

 

var res =parseInt(diffSeconds/3600);
var op=  parseInt(res/24);

 

Hi Harshavardhan,

 

dur I',m getting as 1 00:00:00 for var dur= (sched.duration(new GlideDateTime(start), new GlideDateTime(end)).getDurationValue()); script

 

 

and if I do parseInt(dur/24)     The result will be NaN.

sowmyaj
Giga Expert

based on schedule it is not taking 24 hours, it is considering 8 hours per day but if I divide by using 8hrs / day in a scenario where other schedule contains 9 hours my script will fail.

 

SanjivMeher
Kilo Patron
Kilo Patron

var sched=new GlideSchedule('7aa3e10c8f70010040f82ab2f0f9234d'); //put the sys_id of your schdeule

 

var start='2019-07-25';
var end = '2019-07-29';

var dur= (sched.duration(new GlideDateTime(start), new GlideDateTime(end)).getDurationValue());

 

gs.log(dur.getNumericValue()/3600000/24); //Should give you days


Please mark this response as correct or helpful if it assisted you with your question.