Skip to main content
Known Participant
November 8, 2023
Answered

Comparing dates with JavaScrpt

  • November 8, 2023
  • 1 reply
  • 1798 views

Good morning!  I see everywhere how easy dates are in Java, but I can't seem to get it working.  In a form I'm working on, I need an app.alert to display when the date field "Date_WT2030_Due" is after the date field "Date_of_test".  It would be formatted as mm/dd/yyyy, and those particular fields are drop downs where you can select the date from a little calendar.  I'm pretty sure I need to convert the date to a string first, compare the strings, then convert it back to a date but ¯\_(ツ)_/¯.  Any help would be appreciated!  I can't provide an example of my script. I have so many variations and none of them do anything.  Thanks in advance!

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

Try something like this:

var date1 = this.getField("Date_WT2030_Due").value;
var date2 = this.getField("Date_of_test").value;

if(new Date(date1) > new Date(date2))
app.alert("Date_WT2030_Due is after Date_of_test");

You may also add to reject date or set field to blank.

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
November 8, 2023

Try something like this:

var date1 = this.getField("Date_WT2030_Due").value;
var date2 = this.getField("Date_of_test").value;

if(new Date(date1) > new Date(date2))
app.alert("Date_WT2030_Due is after Date_of_test");

You may also add to reject date or set field to blank.

Known Participant
November 8, 2023

That worked! I think I was making it too complicated.  I just changed date1 = event.value and I'll put it in a bunch of fields. 

Thanks so much!