Expand my Community achievements bar.

SOLVED

Auto calculate end date based on start date

Avatar

Level 5

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

4 Replies

Avatar

Community Advisor

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

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.

Avatar

Correct answer by
Community Advisor

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