Auto calculate end date based on start date | Community
Skip to main content
ReluctantProgrammer
Level 4
Jul 11, 2023 6:42 PM UTC
Solved

Auto calculate end date based on start date

I have a date field named dateStart and a date field named dateEnd.

The dateEnd field needs to automatically fill with the date that is 90 days after the dateStart field, which the user enters.

 

I've tried different options and never get it right.  Can someone please provide a sample of the code to accomplish this?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Vijay_Katoch
Vijay_KatochAccepted solution
New Participant
Jul 13, 2023 5:07 AM UTC

From the dateEnd field in your form, remove validation pattern and make the field protected/readOnly instead of calculated.

 

I have modified the script as per your date fields format:

 

var dtStart = util.scand("yy-mm-dd", this.rawValue);
var dtEnd = new Date(dtStart.getFullYear(), dtStart.getMonth(), dtStart.getDate() +90,0,0,0,0);
var yy = dtEnd.getFullYear().toString();
var mm = (dtEnd.getMonth()+1).toString();
var dd = dtEnd.getDate().toString();
dateEnd.rawValue = (mm[1]?mm:"0"+mm[0]) + "/" +(dd[1]?dd:"0"+dd[0]) + "/" + yy[2]+ yy[3];

4 replies

Vijay_Katoch
New Participant
Jul 12, 2023 10:00 AM UTC

Add below code snippet in the start Date exit event:

 

var startDate= util.scand("yyyy-mm-dd", this.rawValue);
var endDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() +90, 0, 0, 0, 0);
var yyyy = endDate.getFullYear().toString();
var mm = (endDate.getMonth()+1).toString();
var dd = endDate.getDate().toString();
endDateField.rawValue = yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" +(dd[1]?dd:"0"+dd[0]);

Translate
ReluctantProgrammer
Level 4
Jul 12, 2023 12:57 PM UTC

Thanks Vijay but I can't get it to work.  Here's the file.  Maybe that will help. The fields in question have been highlighted.

Translate
Vijay_Katoch
Vijay_KatochAccepted solution
New Participant
Jul 13, 2023 5:07 AM UTC

From the dateEnd field in your form, remove validation pattern and make the field protected/readOnly instead of calculated.

 

I have modified the script as per your date fields format:

 

var dtStart = util.scand("yy-mm-dd", this.rawValue);
var dtEnd = new Date(dtStart.getFullYear(), dtStart.getMonth(), dtStart.getDate() +90,0,0,0,0);
var yy = dtEnd.getFullYear().toString();
var mm = (dtEnd.getMonth()+1).toString();
var dd = dtEnd.getDate().toString();
dateEnd.rawValue = (mm[1]?mm:"0"+mm[0]) + "/" +(dd[1]?dd:"0"+dd[0]) + "/" + yy[2]+ yy[3];

Translate