Date Comparison (DateCompare)

What is it?

The DateCompare function enables you to compare two date strings.

What is the Syntax?

The DateCompare function’s syntax is as follows:

DateCompare( FirstDate, SecondDate );

This function will return an integer value that depends on how the two dates compare:

  • -1 if the first date is earlier than the second.
  • 0 if the two dates are equal.
  • 1 if the first date is later than the second.

Why might I want to use this?

Because the dates compared by this function must be in string format, it is not possible to simply compare them using a standard comparison operator such as greater than (>). Rather, the string must be broken down into its constituent parts and analysed as a date instead of a string.

You may have a questionnaire that allows users to place product orders, and specify the date they would prefer the product to be delivered. At any given time, the earliest delivery time is five days from the date of order. Therefore, if the customer inputs a date that is too soon, or impossible (for example, in the past), they should see a message telling them that they need to input a new, valid date.

ValidOrderDate := DeliveryDate >= DateAdd( "Day", 5, GetDate() );

This expression re-uses the GetDate and DateAdd functions we discussed earlier in this lesson, and returns True only if the value of DeliveryDate is greater than or equal to 5 days after the current date.

 

<< Previous: Add to Date (DateAdd) | Next: Lesson Summary >>