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

New Here ,
May 04, 2018

Copy link to clipboard

Copied

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);

}

TOPICS
Acrobat SDK and JavaScript

Views

66

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

New Here ,
May 04, 2018

Copy link to clipboard

Copied

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);

}

TOPICS
Acrobat SDK and JavaScript

Views

67

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 04, 2018 0

Have something to add?

Join the conversation