How to add duration to date/time using a schedule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 12:04 PM
I have a table that has a duration field on it. I need that duration added to a Date/Time field based on a schedule (which is the 8-5 business days, excluding holidays). I have written the below code, but it doesn't seem to be computing correctly. Can someone offer some advice on what I am doing wrong?
For the duration field, for this example it currently has 16 hours (which would be 2 business days (8 hrs each day)). I have also tried changing the duration to have 2 days but that also did not compute the correct date.
So if I submit the catalog item today (4/4/2022) I want the due date for this example to be 2 business days later so due date should be (4/6/2022). With the below code, I am getting a end date of 4/5/2022.
This code is inside of a script include, which I'm calling from a client script:
getFEWFields: function() {
var item_name;
var obj = {};
var session = gs.getSession();
var zoneName = session.getTimeZoneName();
var item = this.getParameter('sysparm_item');
var startDate = new GlideDateTime(this.getParameter("sysparm_date").toString());
//Get the item name
var name = new GlideRecord('sc_cat_item');
name.addQuery('sys_id', item);
name.query();
if (name.next()) {
item_name = name.name;
gs.log('Item name' + item_name);
}
var few_deets = new GlideRecord('u_few_item_details');
few_deets.addQuery('u_name', item_name);
few_deets.query();
if (few_deets.next()) {
obj.bin = few_deets.getDisplayValue('u_bin_location').toString();
obj.unit = few_deets.getDisplayValue('u_unit_of_issue').toString();
var dur = new GlideDateTime(few_deets.u_lead_time.getValue());
dur = dur.getNumericValue() / 1000; //time in seconds
var dc = new DurationCalculator();
dc.setTimeZone(zoneName);
dc.setSchedule('090eecae0a0a0b260077e1dfa71da828'); //8-5 weekdays, excluding holidays
dc.setStartDateTime(startDate);
dc.calcDuration(dur);
var endDate = dc.getEndDateTime();
gs.log('Item endDate' + endDate + 'End Date' +endDate.getDisplayValue());
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 01:53 PM
Hello,
Your code for calculating the date from schedule is correct but I'd suggest checking the value being passed with the sysparm_date parameter. My guess is that value which is being passed isn't in the correct timezone so that when you are calling 'dc.setStartDateTime(startDate)' it isn't properly accounting for the timezone thus providing you the wrong end date.
Hope this helps point you in the right direction.
--David