local datetime function to account for daylight savings time

ritaaudi
Tera Contributor

Hi; I have the following script include which returns a glide date that populates a local time(US/Eastern) field on a Change template form. As you can see below I am using the getDSTOffset() function to account for daylight savings time. This works, however, I am wondering if there is a more efficient way of doing this using the Local datetime functions. I tried the addSecondsLocalTime() but that doesn't seem to work. Please advise. Thanks, Rita function PopulateChangeScheduleDates() {   var gdtday = new GlideDateTime(gs.now() + " 07:00:00");   var offset = gdtday.getDSTOffset();   if (offset > 0) {     gdtday.addSeconds(-3600);   }   gdtday.addDays(1);   var runDate = gdtday.getValue();   return runDate; }

3 REPLIES 3

mrswann
Kilo Guru

I am not sure I understand what you are trying to achieve.



We experience some issues each year around the spring & winter clock changes, but only really manifests itself when I export XML as this all comes out in UTC.


Everything I see through the UI including reports is displayed in my local timezone.



Could you provide some context to what your desired outcomes are and how the issue is presenting itself? I may be missing a trick, or this could be a look out for future enhancements as we are not currently utilising schedules much.


ritaaudi
Tera Contributor

Hi: Sure, let me elaborate. Basically in the script include, we are doing date time calculation, but with that we are having to add 4 hours to the GlideDateTime in order to account for the time difference. Also this time gets shifted by an hour every Fall and Spring when the time changes. So we are having to manipulate the script to account for the 4 hour difference and use the getDSTOffset() to subtract or add an hour for daylight savings time.



I am wondering if there is a better way to do this using the local datetime functions introduced in Eureka and after versions so we don't have to add and subtract hours from the GlideDateTime to use the actual local date time.



Thank you. Rita


joshua_huff
Kilo Contributor

Rita,



This is how I ended up handling it.


Allows for it to automatically adjust without having to check if it is 0 or not.



var dateTime = new GlideDateTime();


var timeAdjust = 1000 * 60 * 60 * 5;


dateTime.subtract(timeAdjust);


dateTime.add(dateTime.getDSTOffset());


return dateTime;