How does GlideDuration work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2021 06:05 AM
I'm having trouble understanding how does glideduration actually work.
I have a sample code:
Both end dates is returning 1day 3hrs (27 hours or 9 business days based from schedule).
Shouldn't the first endDate duration show 22hours, since 01:00 is outside business hours and counting of hours will resume at 08:00 which would then complete the 27 hours at 13:00. Or am I missing something here?
I'd appreciate any help. Thanks.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2021 06:57 AM
Hi Kirby,
I have just tried the script which you have posted and I think its working fine and giving the desired outputs also
var startDate = new GlideDateTime('2021-02-08 13:00:00');
var endDate = new GlideDateTime('2021-02-11 01:00:00');
var endDate2 = new GlideDateTime('2021-02-11 13:00:00');
var dc = new DurationCalculator();
var dc2 = new DurationCalculator();
var dur = dc.calcScheduleDuration(startDate,endDate) * 1000;
var dur2 = dc2.calcScheduleDuration(startDate,endDate2) * 1000;
gs.info(new GlideDuration(dur).getDurationValue());
gs.info(new GlideDuration(dur2).getDurationValue());
Outputs :
2021-02-15T14:50:03.109Z:
"2 12:00:00"
2021-02-15T14:50:03.110Z:
"3 00:00:00"
Some information on the GlideDuration API which could be helpful for reference.
There are currently a few places in the system where the user needs to specify when something is due.
Often defining when something is due is really indicating the amount of work time that is required
for completion.
SLAs and Workflow tasks and approvals are places that require specifying amount of work or when something
is due.
Expressing how much work is required is often restricted by a calendar and time zone such that only part
of the day is considered when determining when the work is to be performed. For example,
Consider work that is due in 10 hours, but we are restricted to a business day
schedule. The 10 hours of work can only be done at most 8 hours each day, so if
this work starts at 10 am on Monday, it is expected to complete, or be due, on Tuesday
at 12 pm:
10 am-5 pm on Monday (6 hours)
+ 8 am-12 pm on Tuesday (4 hours)
Expressing when something is due or how much work is required may also need to be specified
as a 'relative duration'. Examples of relative durations include:
Next business day by 4 pm
2 business days by 10:30 am
Determining the expected due date and time is relative to the starting time and the calendar
and time zone being used. The calendar is used to determine what 'next business day' means since
it is the calendar that defines which days are valid days for work to be performed. As an example,
consider 'Next business day by 4 pm':
If it is Monday at 12 pm:
Next business day by 4 pm => Tuesday at 4 pm
If it is Friday at 2 pm:
Next business day by 4 pm => the following Monday at 4 pm
Note: Next business day is often defined with a starting time day (Next business day
at 4pm if before 2pm). This is used to indicate that if we are after 2pm on a business
day, then 'Next business day' really means 2 business days since today does not count.
Usage:
var dc = new DurationCalculator();
// Settings for calculations
// (optional: specify the Schedule to use for the following calculations)
dc.setSchedule(/* String cmn_schedule.sys_id */ scheduleId, /* String */ timezone);
// (optional: specify a different timezone to use)
dc.setTimeZone(/* String */ timezone);
// (optional: otherwise current time is assumed)
dc.setStartDateTime(/* GlideDateTime or UTC Time String */, start);
// Calculate end time, from number of seconds required in the schedule
dc.calcDuration(t);
// Calculate a relative duration end time, seconds until end time
// (evaluates the relative duration script - http://docs.servicenow.com/?context=Defining_Relative_Durations)
dc.calcRelativeDuration(/* String cmn_relative_duration.sys_id */ relativeDurationId);
// Calculate the number of seconds, in the specified schedule, between two times
// NB. returns 0 if endTime is before startTime
dc.calcScheduleDuration(/* GlideDateTime or UTC Time String */ startTime, /* GlideDateTime or UTC Time String */ endTime);
// retrieve results
// (the ending time, after t seconds)
var endTime = dc.getEndDateTime();
// (the number of seconds, inside the schedule)
var seconds = dc.getSeconds;
// (the total number of seconds until the endTime)
var totalSeconds = dc.getTotalSeconds();
// other useful methods
var isAfter = dc.isAfter(/*GlideDateTime*/ dateTime, /*String hh:mm:ss*/ time);
Please mark the answer correct if it was helpful.
Regards,
Amit Gujarathi
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi