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.

i want to add years to a date field.....but addYearsLocalTime() & addYearsUTC are not working,so i tried to add years by converting them into seconds and then addSeconds but that one is also not working in an application scope ......

rajputgagan38
Kilo Explorer

here is the code i wrote !!

every output is as expected . but the variable "add" is showing output as undefined. unable to add seconds to it.
m writing this code in an application scope.

var warranty_date=new GlideDateTime('2017-12-02 00:00:00'); //warranty date in database

var find_year=warranty_date.getYearLocalTime();

var yrs=2; //value of new warranty years

var secs=0;

                              var tot=find_year+yrs;

for(var i=find_year;i<=tot;i++)

{

var days_in_year=(i%4==0)?366:365;

secs+=days_in_year*86400;       // sec in a day*total days in a year  

}

var new_day=new GlideDateTime(warranty_date);

var add=new_day.addSeconds(secs);

gs.info("new: "+add);

1 REPLY 1

karthik73
Mega Guru

Hi Gagan,



Can you please try this code?



var warranty_date=new GlideDateTime('2017-12-02 00:00:00'); //warranty date in database


var find_year=warranty_date.getYearLocalTime();


var yrs=2; //value of new warranty years


var secs=0;


warranty_date.addYearsLocalTime(yrs);


gs.print(warranty_date.getDate());