Calculate date + 30 days then first of following month

Community Beginner ,
Jun 16, 2017

Copy link to clipboard

Copied

I have a calculated field in my form that adds 30 days to a date entered into DateofHire. I need the resulting date to be the first of the following month.

Ex:

Using a hire date of 7/1/17 my script results in 7/31/17, I need to calculate it to 8/1/17.

If the hire date is 7/2/17, the script results in 8/1/17, I need the field to go to 9/1/17.

I hope that makes sense. Here's my current script:

// Custom Calculate script for NewDate field

(function () {

    // Get date entered into the DOH field

    var sDate = getField("DateofHire").valueAsString;

    // Convert string to date

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

   // Add days to date

    d.setDate(d.getDate() + 30);

    // Populate this field with the result

    if (sDate) {

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

    } else {

        event.value = "";

    }

})();

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change this line:

d.setDate(d.getDate() + 30);

To:

d.setDate(d.getDate() + 30);

if (d.getMonth()==11) {

    d.setMonth(0);

    d.setFullYear(d.getFullYear()+1);

} else d.setMonth(d.getMonth()+1);

d.setDate(1);

TOPICS
Acrobat SDK and JavaScript, Windows

Views

1.2K

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

Calculate date + 30 days then first of following month

Community Beginner ,
Jun 16, 2017

Copy link to clipboard

Copied

I have a calculated field in my form that adds 30 days to a date entered into DateofHire. I need the resulting date to be the first of the following month.

Ex:

Using a hire date of 7/1/17 my script results in 7/31/17, I need to calculate it to 8/1/17.

If the hire date is 7/2/17, the script results in 8/1/17, I need the field to go to 9/1/17.

I hope that makes sense. Here's my current script:

// Custom Calculate script for NewDate field

(function () {

    // Get date entered into the DOH field

    var sDate = getField("DateofHire").valueAsString;

    // Convert string to date

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

   // Add days to date

    d.setDate(d.getDate() + 30);

    // Populate this field with the result

    if (sDate) {

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

    } else {

        event.value = "";

    }

})();

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change this line:

d.setDate(d.getDate() + 30);

To:

d.setDate(d.getDate() + 30);

if (d.getMonth()==11) {

    d.setMonth(0);

    d.setFullYear(d.getFullYear()+1);

} else d.setMonth(d.getMonth()+1);

d.setDate(1);

TOPICS
Acrobat SDK and JavaScript, Windows

Views

1.2K

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
Jun 16, 2017 0
Most Valuable Participant ,
Jun 16, 2017

Copy link to clipboard

Copied

Change this line:

d.setDate(d.getDate() + 30);

To:

d.setDate(d.getDate() + 30);

if (d.getMonth()==11) {

    d.setMonth(0);

    d.setFullYear(d.getFullYear()+1);

} else d.setMonth(d.getMonth()+1);

d.setDate(1);

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
Reply
Loading...
Jun 16, 2017 1
Adobe Community Professional ,
Jun 16, 2017

Copy link to clipboard

Copied

I created a Date JavaScript library for just these sorts of things. You can read about it here...

http://practicalpdf.com/the-practicalpdf-date-library-for-adobe-acrobat/

The nextMonth method will allow you to advance the date to the first day of the next month and optionally set it to the first business day of that month assuming a Monday through Friday work week.

Date - Extended Date Object for Adobe Acrobat 

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
Reply
Loading...
Jun 16, 2017 0
r_armont LATEST
Community Beginner ,
Apr 13, 2018

Copy link to clipboard

Copied

I know this is late, but thank you try67, that was what I was looking for!!

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
Reply
Loading...
Apr 13, 2018 0