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

Dividing by 24 gives me 1 day which is wrong based on the dates I have entered. It should be 3 and I get the answer if I divide by 8 instead of 24 but then it is kind of hardcoding for specific schedule

Where do you want to use the number of days? Can you elaborate the complete use case?


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

On the custom field which is of string type. It should populate the number of working days between the closed date (date time type) of projects and the another custom field(date type). For this it is using OOB schedule “Project management schedule”

 

abc = closed date - xyz should give number of working days using project management schedule 

Ok. I think what you need to do is query the schedule entry and pull the start and end date from there as well to get the number of hours per day.

For ex, below is a schedule entry where start and end date difference is 9 hours. you can get the difference and then divide it from the dur you got above to get the number of days

find_real_file.png


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

To be simplify if my start date (custom field) is 2019-07-25 and closed date (system closed date) is 2019-07-29 the difference I need to get as 3 days not 5days as in 5days it includes weekend and in the above script I get the result in duration