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.

Add months to GlideDate

thisisauniqueus
Giga Expert

Hi

I want to add months to my start date until a target date like for example

var startDate = new GlideDate();

var endDate = new GlideDate();

endDate.setValue('2016-09-09');

while(startDate<=endDate){

//do some processing

startDate.addMonth(1); //this is not an actual method just added here for verbosity

}

basically i have to generate a record for every month between the startDate and endDate

Regards

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi John,



It's not a clean solution, but you could convert the GlideDate object to a GlideDateTime and use addMonthsLocal() or addMonthsUTC(), then convert back to a GlideDate().



It's a bit messy. I'm not sure why GlideDate() doesn't have addDays(), addMonths(), and addYears() - or some variant of that. I wish it did and invite you to open an enhancement request.



Enhancement requests: Tell us how you would improve the ServiceNow product


View solution in original post

17 REPLIES 17

Try this in your script include on line 11 and 12 instead.



var d =   gd.getByFormat("dd/MM/yyyy");



return d;


Thanks Chuck, I copied the GlideDate() value to a GlideDateTime(), then added the   specified months. Later used setDisplayValue("dd/MM/yyyy HH:mm:ss") and then getDisplayValue() while retrieving the data back to the client script.



Quite an elaborate process I must say just to add few days to a GlideDate() field...:)



Cheers


Suhas


Hi Suhas,



I know - I don't like changing data types like that either just to add a few days. It would be great if the GlideDate class had some of the methods available to GlideDateTime.



I invite you to open an enhancement request! Our product managers DO listen.


Enhancement requests: Tell us how you would improve the ServiceNow product


Hi Suhas,



Please check response in How to convert date format, if it helps.


Midhun1
Giga Guru

Hi John,



Examle:



var inc = new GlideRecord('incident');


inc.addActiveQuery();


inc.addQuery('number','INC0000055');


inc.query();


var date="";


if(inc.next()){


date=inc.opened_at;


gs.print(date);


var datee = new GlideDate();  


datee.setValue(date);


datee.addMonths(2);


gs.print(datee.getDate());


}



output:


[0:00:00.004] Script completed in scope global: script



*** Script: 2016-06-30 04:47:23
*** Script: 2016-08-30