GlideDateTime API: addDays and addMonths

User173809
Tera Contributor

Can someone explain the difference between the following functions in the GlideDateTime API? I'm failing to understand how timezones play a part in day, month or year adjustments.

I've run some background scripts to try and see if there is any difference and it appears that they do the exact same thing. So what then is the purpose of breaking them out?

 

addDaysUTC()

addDaysLocalTime()

addMonthsUTC()

addMonthsLocalTime()

3 REPLIES 3

Omkar Mone
Mega Sage

Hi 

addDaysUTC() - Adds a specified number of days to the current GlideDateTime object. A negative parameter subtracts days. The method determines the UTC date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts days using the UTC date and time values.

Ex-

var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addDaysUTC(-1);
gs.info(gdt.getDate());

 

addDaysLocalTime() - Adds a specified number of days to the current GlideDateTime object. A negative parameter subtracts days. The method determines the local date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts days using the local date and time values.

Ex-

var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addDaysLocalTime(-1);
gs.info(gdt.getLocalDate());

addMonthsUTC() - Adds a specified number of months to the current GlideDateTime object. A negative parameter subtracts months. The method determines the UTC date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts months using the UTC date and time values.

Ex-

var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addMonthsUTC(2);
gs.info(gdt.getDate());

 

addMonthsLocalTime() - Adds a specified number of months to the current GlideDateTime object. A negative parameter subtracts months. The method determines the local date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts months using the local date and time values.

Ex-

var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addMonthsLocalTime(2);
gs.info(gdt.getDate());


Mark correct if it helps.



Regards,

Omkar Mone

www.dxsherpa.com

 

David Stutter
Tera Guru

It could be relevant for daylight saving time but only on edge cases.

For example:

var gdt = new GlideDateTime("2011-07-20 23:30:00");
gdt.addMonthsLocalTime(3);
gs.info(gdt.getLocalDate());

>>> 2018-08-21T13:59:41.606Z: 2011-10-21

 

Location: Austria

 

John Basso
Tera Contributor

I know that the last post is years old, but I had the same question, and I believe that David's response about the Daylight Savings Time edge case answered it.
The switch from Standard Time to Daylight Savings Time occurred on March 12, 2023, so I used a DateTime of March 12, 2023 @ 02:15 UTC in the test code shown below.

Timezone for the instance is US/Eastern.

 

Background script:

 

var exampleDateTime = '2023-03-12 2:15:00';

var testDate1 = new GlideDateTime();
testDate1.setValue(exampleDateTime);

var testDate2 = new GlideDateTime();
testDate2.setValue(exampleDateTime);

gs.print('testDate1 before: ' + testDate1);
gs.print('testDate2 before: ' + testDate2);

testDate1.addDaysLocalTime(1);
testDate2.addDaysUTC(1);

gs.print('testDate1 after: ' + testDate1);
gs.print('testDate2 after: ' + testDate2);

 

 

 

Results:

 

*** Script: testDate1 before: 2023-03-12 02:15:00
*** Script: testDate2 before: 2023-03-12 02:15:00
*** Script: testDate1 after: 2023-03-13 01:15:00
*** Script: testDate2 after: 2023-03-13 02:15:00