Skip to main content
Lyle_Worsley
Participating Frequently
June 5, 2021
Answered

Javascript auto fill in date fields from reference field

  • June 5, 2021
  • 1 reply
  • 2308 views

I have a expense form that lists each day of the week. The form has a main date field that the user selects the Saturday date from a drop down calendar. I would like to have the individual days to have the date autofill. Does anyone know the Javascript that will calculate a date from an existing field.

 

Example  If Main Date Field is entered 6/5/2021 (always a Saturday Date), I would like the previous days date to autofill.  Sunday Date field would auto fill with the date 5/30/2021, Monday Date field 5/31/2021, etc for each day of the week through Saturday showing 6/5/2021.  Is this possible?

 

Thanks in advance.

This topic has been closed for replies.
Correct answer Nesa Nurani

Here is some basic script you can easily adapt (if you wish so) I set field names as week days, you can change that to your actual field names, use script as validation script of "Saturday" field:

if(event.value == ""){
this.getField("Friday").value = "";
this.getField("Thursday").value = "";
this.getField("Wednesday").value = "";
this.getField("Tuesday").value = "";
this.getField("Monday").value = "";
this.getField("Sunday").value = "";}
else{
var d1 = util.scand("m/d/yyyy", event.value)
d1.setDate(d1.getDate() -1);
var d2 = util.scand("m/d/yyyy", event.value)
d2.setDate(d2.getDate() -2);
var d3 = util.scand("m/d/yyyy", event.value)
d3.setDate(d3.getDate() -3);
var d4 = util.scand("m/d/yyyy", event.value)
d4.setDate(d4.getDate() -4);
var d5 = util.scand("m/d/yyyy", event.value)
d5.setDate(d5.getDate() -5);
var d6 = util.scand("m/d/yyyy", event.value)
d6.setDate(d6.getDate() -6);

this.getField("Friday").value = util.printd("m/d/yyyy", d1);
this.getField("Thursday").value = util.printd("m/d/yyyy", d2);
this.getField("Wednesday").value = util.printd("m/d/yyyy", d3);
this.getField("Tuesday").value = util.printd("m/d/yyyy", d4);
this.getField("Monday").value = util.printd("m/d/yyyy", d5);
this.getField("Sunday").value = util.printd("m/d/yyyy", d6);}

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
June 5, 2021

Here is some basic script you can easily adapt (if you wish so) I set field names as week days, you can change that to your actual field names, use script as validation script of "Saturday" field:

if(event.value == ""){
this.getField("Friday").value = "";
this.getField("Thursday").value = "";
this.getField("Wednesday").value = "";
this.getField("Tuesday").value = "";
this.getField("Monday").value = "";
this.getField("Sunday").value = "";}
else{
var d1 = util.scand("m/d/yyyy", event.value)
d1.setDate(d1.getDate() -1);
var d2 = util.scand("m/d/yyyy", event.value)
d2.setDate(d2.getDate() -2);
var d3 = util.scand("m/d/yyyy", event.value)
d3.setDate(d3.getDate() -3);
var d4 = util.scand("m/d/yyyy", event.value)
d4.setDate(d4.getDate() -4);
var d5 = util.scand("m/d/yyyy", event.value)
d5.setDate(d5.getDate() -5);
var d6 = util.scand("m/d/yyyy", event.value)
d6.setDate(d6.getDate() -6);

this.getField("Friday").value = util.printd("m/d/yyyy", d1);
this.getField("Thursday").value = util.printd("m/d/yyyy", d2);
this.getField("Wednesday").value = util.printd("m/d/yyyy", d3);
this.getField("Tuesday").value = util.printd("m/d/yyyy", d4);
this.getField("Monday").value = util.printd("m/d/yyyy", d5);
this.getField("Sunday").value = util.printd("m/d/yyyy", d6);}

Lyle_Worsley
Participating Frequently
June 13, 2021

It works.  Thanks!