How to calculate duration in days using schedule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 10:28 AM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 11:02 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 11:39 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 01:03 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 01:17 PM
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
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2019 09:29 PM