DurationCalculator & GlideDateTime

snuser2708
Kilo Contributor

Having trouble using the DurationCalculator class and it's methods. I am trying to use it to calculate the duration difference between current date_time and the planned start date_time on a change record. I am using the setSchedule method to set the schedule to the OOB 8-5 weekdays schedule & subsequently calculating the time difference (between current time and change start_date_time) based on this schedule using calcScheduleDuration method. For example, if current time is 4PM and the change start date is tomorrow @ 10 AM then the duration calculator above should calculate a time difference of 3 hrs based on the 8-5 schedule. Also when I initiate the GlideDateTime object with current date and time, how do I force the GlideDateTime to use the session time zone and not the GMT time?

1 ACCEPTED SOLUTION

Stijn Verhulst3
Kilo Guru

Hi,



this can be done by calculating the timezone offset and adding it to your calculated date/time. If the offset is negative it will be automatically subtracted instead of added.



The caclDate variable in the script below represents your current calculated date time in GMT time:



// Calculate timezone offset


var gdt = new GlideDateTime();


gdt.getLocalTime();



// Add timezone offset to GMT date


calcDate.add(gdt.getTZOffset());



Hope this helps you forward,



Stijn


View solution in original post

3 REPLIES 3

lokendarsingh
Giga Contributor

Initialise GlideDateTime by passing the gs.nowDateTime()



example are:


//This will return the date in User's timezone


var gdt = new GlideDateTime(gs.nowDateTime());


gs.print(gdt);



// This will return the value in GMT


var gdt = new GlideDateTime();


gs.print(gdt);


Hmmm, running this at 3:28 p.m. under my Paris instance gives this:

*** Script: Local Date & time is 2021-07-16 03:28:24
*** Script: GMT Date & time is 2021-07-16 19:28:24

The time formats don't match; if they were the same, the local time would be 3:28 a.m.
If the formats were the same, the local time should have been 15:28:24.

Stijn Verhulst3
Kilo Guru

Hi,



this can be done by calculating the timezone offset and adding it to your calculated date/time. If the offset is negative it will be automatically subtracted instead of added.



The caclDate variable in the script below represents your current calculated date time in GMT time:



// Calculate timezone offset


var gdt = new GlideDateTime();


gdt.getLocalTime();



// Add timezone offset to GMT date


calcDate.add(gdt.getTZOffset());



Hope this helps you forward,



Stijn