Skip to main content
January 5, 2017
Question

Coding Less Than Sign

  • January 5, 2017
  • 2 replies
  • 912 views

Can you code a less than sign. If a date is entered before 1/1/2017, I would like to create an alert.  This is the script I came up with but it's not working and I can't find anything out there on the internet for it.

if(event.value > "01/01/2017"){

app.alert("Please confirm the date entered",1);

Thanks.

This topic has been closed for replies.

2 replies

JR Boulay
Community Expert
Community Expert
January 5, 2017
Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
January 5, 2017

You can only use these operators on numbers, not on strings.

To compare dates is a more complex process. You need to first convert the strings to a Date object (using util.scand), and then you'll be able to access their getTime method, which returns a number of milliseconds for that date, which you can then compare with another date.

Inspiring
January 8, 2017

The ">" and "<" operators will work on strings and it is used in that manner to preform an alphabetic sort.

From the MDN JavaScript Reference for JavaScript:

Strings are compared based on standard lexicographical ordering, using Unicode values.

Features of comparisons:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two distinct objects are never equal for either strict or abstract comparisons.
  • An expression comparing Objects is only true if the operands reference the same Object.
  • Null and Undefined Types are strictly equal to themselves and abstractly equal to each other.

Note if you compare date strings they are not compared and numerical values but strict string values. You need to convert the date string into  a numeric value that represents that date in the proper numeric sequence of all dates. JavaScript does this with the date object where each millisecond since January 1, 1970 12:00 am is a assigned a raising numeric value so each date has an ascending unique value.

Try the following script in the JavaScript console:

var dateCompare = "19700101" == "January 1, 1970";
console.println("\"19700101\" == \"January 1, 1970\" " + dateCompare);
dateCompare = util.scand("yyyymmdd", "19700101").getTime() == util.scand("mmmm dd, yyyy", "January 1, 1970").getTime();
console.println("JSO date for \"19700101\" to JOS date for \"January 1, 1970\" " + dateCompare);

and observe the result.

Inspiring
January 8, 2017

You're right, it's possible to compare strings as strings (which is basically a character code comparison), but that's not what OP was trying to do, of course. He/she was trying to compare them as dates, which is not going to work.


The point is that the dates need to reduced or converted to the numeric system.