Skip to main content
Participant
July 12, 2023
Question

How do I set a date field based on another field with a blank default

  • July 12, 2023
  • 1 reply
  • 257 views

I've got a script to set a date field  (LastDayWorked) as one day before another date field (TermDate) but when I clear out the original TermDate the LastDayWorked auto defaults to the day before the current date.  I'm not sure how to adjust the script so that if the TermDate field is blank then so is the LastDayWorked as I only want a date populating there if there is a TermDate entered. Any suggestions on how to clear this field?  I've currently got:

 

var numDaysToAdd = -1 ;
var fromDate = util.scand("yyyy-mm-dd", this.getField("TermDate").value) ;
var toDate = fromDate ;
toDate.setDate(fromDate.getDate() + numDaysToAdd) ;
event.value = util.printd("yyyy-mm-dd", toDate) ;

 

Greatly appreciate any/all insight! Thank you!

This topic has been closed for replies.

1 reply

Nesa Nurani
Community Expert
Community Expert
July 12, 2023

Use this:

var fromDate = util.scand("yyyy-mm-dd", this.getField("TermDate").valueAsString);
if(this.getField("TermDate").valueAsString === "")
event.value = "";
else{
fromDate.setDate(fromDate.getDate() -1) ;
event.value = util.printd("yyyy-mm-dd", fromDate);}