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

Try the following:




var gd = new GlideDateTime();  


gd.addMonthsUTC(parseInt(9));  


gs.print(gd.getValue());  


upasanamahanta
Mega Contributor

Try using gs.nowDateTime() as follows->




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


gd.addMonthsLocalTime(parseInt(9));  


gs.print(gd.getValue());  



You get the correct value then.


prasanna11
Giga Guru

To add months to a date object,



  • converted it to a date time object.
  • Added months using addMonthsUTC
  • Set the value to date object. ( i hope date time to date conversion happens internally while such assignment)