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

Ok..I will write the code to tell what I am trying to say

 

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

 

// Query the schedule span, which holds the duration of the schedule

var sched1 = new GlideRecord('cmn_schedule_span');

sched1.addQuery('schedule','7aa3e10c8f70010040f82ab2f0f9234d');

sched1.query();

 

if (sched1.next())

{

var date1 = new GlideDateTime();
var date2 = new GlideDateTime();
date1.setDisplayValueInternal(sched1.start_date_time);
date2.setDisplayValueInternal(sched1.start_date_time);

// Determine the difference as number of seconds (returns a string)
// Use getDisplayValue() to convert the string to the format expected by dateDiff()
var diffHours = gs.dateDiff(date1.getDisplayValue(), date2.getDisplayValue(), true)/60;

}

 

var no_of_days = dur.getNumericValue()/3600000/24/diffHours; // Your number of days


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