Skip to main content
Pepperminthippo
Participant
May 4, 2018
Question

Add month(s) to date, not to exceed the last day of the next month

I'm trying to add one or more months to a date field, but I'm running into an issue with the end of the month. If "today" is 01/31/2018, and I add one month, I would like the result to be 02/29/2018, but instead I get 03/03/2018.

Is there a way to calculate this, check to see if the date matches, and if it doesn't, set the date at the end of the previous month? In my example, 31!=3, so set the date at 02/29/2018 instead of 03/03/2018.

Here's the script that I'm using:

todayString = this.getField("today").valueAsString;

var months = Number(this.getField("months").valueAsString);

if (todayString=="" || months==0) event.value = "";

else {   

    var d = util.scand("dd.mm.yyyy", startDateString);

    d.setMonth(d.getMonth()+months);

    event.value = util.printd("dd.mm.yyyy", d);

}

This topic is closed to new replies. Start a new post to keep the conversation going.