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

While it appears you are still able to use addMonths() to the GlideDate() class, this is deprecated and should be avoided as it does not work with the scoped API.



Reference: Scoped GlideDate API Reference - ServiceNow Wiki


Thanks Midhun for your time and effort



Regards


Midhun1
Giga Guru

Hi Chuck,



Thanks!! for the information.