Skip to main content
ReluctantProgrammer
Level 4
July 11, 2023
解決済み

Auto calculate end date based on start date

  • July 11, 2023
  • 4 の返信
  • 1026 ビュー

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?

このトピックへの返信は締め切られました。
ベストアンサー Vijay_Katoch

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 の返信

Vijay_Katoch
Community Advisor
Community Advisor
July 12, 2023

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

ReluctantProgrammer
Level 4
July 12, 2023

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.

Vijay_Katoch
Community Advisor
Community Advisor
July 13, 2023

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