Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how does addMonthsLocalTime() work?

prasanna11
Giga Guru

Does this function calculates result based upon number of days/ just plays with the months part ? I see ambiguity as below..

gs.print(gs.now());

var gd = new GlideDateTime();

gd.setValue(gs.now());

gd.addMonthsLocalTime(parseInt(9));

gs.print(gd.getValue());

Output:

*** Script: 2017-01-06

*** Script: 2017-10-05 23:00:00

Whereas replacing number of months with 10 produces below result.

*** Script: 2017-01-06

*** Script: 2017-11-06 00:00:00

Why the one hour ambiguity when its 9 months?

1 ACCEPTED SOLUTION

Try the following:




var gd = new GlideDateTime();  


gd.addMonthsUTC(parseInt(9));  


gs.print(gd.getValue());  


View solution in original post

7 REPLIES 7

Melghi
Kilo Expert

It's an addition, since we are in January it's 1+9 when you put


  1. gd.addMonthsLocalTime(parseInt(9));  


And it's 1+10 = 11 when you do


  1. gd.addMonthsLocalTime(parseInt(10));  

Yes but the date is one day earlier when its 9 months


Try nowDateTime() instead of now()


Hi Mohamed,



It works when I use datetime object. However i have necessity to add months to a date object which i get from record and nt a datetime.



Is it possible to add months to a date object without messing up the dates using OOB methods?