Calculating end date with time zones

Jake Sadler
Kilo Sage

I currently have the script below and I'm trying to add duration to the start date of a task to calculate the end date. The issue I'm having is, how do I take into consideration the timezone difference. So, if I schedule the start date of the task to March 30th at 9am GMT time, the display value will show 9am but the XML value shows 8am. 

If we say that duration is equal to 1 hour then we should see the end date as 10am however, as it grabs the value in UTc the duration of 1 hour is being added to 8am and so the start date and end date both show 9am. How can I get the duration to add to the GMT time?

 

 

(function executeRule(current, previous /*null when async*/) {



var gdt = new GlideDateTime(current.start_date.getValue());
var ms = current.duration.dateNumericValue();
gdt.add(ms);
current.end_date = gdt.getValue();

 

})(current, previous);

1 ACCEPTED SOLUTION

Jake Sadler
Kilo Sage

(function executeRule(current, previous /*null when async*/) {
var tz = new GlideDateTime();
tz.setDisplayValue(current.start_date.getDisplayValue(),"dd-MM-yy hh:mm:ss");

if(tz.isDST()){

var gdt = new GlideDateTime(current.start_date.getValue());
var ms = current.duration.dateNumericValue();
var msTotal = ms + 3600000;
gdt.add(msTotal);
current.end_date = gdt.getValue();

}else{

var gdt1 = new GlideDateTime(current.start_date.getValue());
var ms1 = current.duration.dateNumericValue();
gdt1.add(ms1);
current.end_date = gdt1.getValue();
}

 

})(current, previous);

View solution in original post

2 REPLIES 2

Vishnu Prasad K
Giga Guru

Hi Jsadlet,

 

You can use addDaysUTC or addDaysLocalTime. In your case since you don't need UTC, use addDaysLocalTime

Jake Sadler
Kilo Sage

(function executeRule(current, previous /*null when async*/) {
var tz = new GlideDateTime();
tz.setDisplayValue(current.start_date.getDisplayValue(),"dd-MM-yy hh:mm:ss");

if(tz.isDST()){

var gdt = new GlideDateTime(current.start_date.getValue());
var ms = current.duration.dateNumericValue();
var msTotal = ms + 3600000;
gdt.add(msTotal);
current.end_date = gdt.getValue();

}else{

var gdt1 = new GlideDateTime(current.start_date.getValue());
var ms1 = current.duration.dateNumericValue();
gdt1.add(ms1);
current.end_date = gdt1.getValue();
}

 

})(current, previous);