Skip to main content
Participating Frequently
Apr 5, 2025 3:38 PM UTC
Answered

Calculate expiration date 14 months from selected date

Hello! I'm trying to figure out how to calculate 14 months from a date in a previous field. I currently have it set up so the "Today" date is autmatically populated upon opening, and need the expiration date to autopupulate 14 months in the future from that date. I have the below script but it comes up with compeltely random numbers. Thank you for the help!

 

if (event.target.value != "") { // check if populated else script will fail.

var d = new Date(event.target.value); // gets selected date from "Today" field

var DateNextYear = new Date(d.getMonth(), d.getDate(), d.getFullYear() +1);

var fld = this.getField("Expiration");

fld.value = util.printd("mm/dd/yy",DateNextYear); //assign future date to "Expiration" field

//Adjust the field formatting in "fld.value" to match the formatting set for the field. In this case I used "mm/dd/yy".

}

try67
try67Correct answer
Community Expert
Apr 16, 2025 2:29 PM UTC

One more question, how would this script change if we wanted to do 420 days from the "today" field? Thanks again for the help!


Change:

DateNextYear.setMonth(d.getMonth() + 14);

To:

DateNextYear.setDate(d.getDate() + 420);

10 replies

try67
Community Expert
Community Expert
Apr 5, 2025 3:58 PM UTC

Replace this:

var DateNextYear = new Date(d.getMonth(), d.getDate(), d.getFullYear() +1);

With:

var DateNextYear = new Date(d.getTime());

DateNextYear.setMonth(d.getMonth()+14);

Translate
Participating Frequently
Apr 5, 2025 4:07 PM UTC

thank you so much for the help! I'm still getting some pretty strange numbers and wondering what setting could be off?

Below is the current script I have:

 

if (event.target.value != "") { // check if populated else script will fail.
var d = new Date(event.target.value); // gets selected date from "Today" field
var DateNextYear = new Date(d.getTime());
    DateNextYear.setMonth(d.getMonth()+14);
var fld = this.getField("Expiration");
fld.value = util.printd("mm/dd/yy",DateNextYear); //assign future date to "Expiration" field
//Adjust the field formatting in "fld.value" to match the formatting set for the field. In this case I used "mm/dd/yy".
}
Translate
try67
Community Expert
Community Expert
Apr 5, 2025 5:44 PM UTC

Use this code as the custom Validation script of the "Today" field:

 

var expFld = this.getField("Expiration");
if (event.value != "") { // check if populated else script will fail.
    var d = util.scand("mm/dd/yy", event.value); // gets selected date from "Today" field
    var DateNextYear = new Date(d.getTime());
    DateNextYear.setMonth(d.getMonth() + 14);
    expFld.value = util.printd("mm/dd/yy", DateNextYear); //assign future date to "Expiration" field
} else expFld.value = "";
Translate