compare dates

chestera
chestera used Ask the Experts™
on
Hi EE

I have If date1< date 2 do something.
Date1 = 06-nov-2015
Date2 = 01-Jan-2016
My question why  is Date1 coming up as greater than date two.
Any help appreciated

chestera
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017
Commented:
Because you have formatted the date and formatting a date turns it into a string which makes it behave like a string and not like a date.  Strings are compared character by character, left to right so - 0 = 0 but 6 > 1 and the compare stops at that point.  Dates are actually numeric data types and so they are compared as numbers.  The two arguments are aligned at the decimal point and the compare goes right to left on the significant digits and left to right on the decimal portion.

The bottom line is:
NEVER format a date EXCEPT for display purposes.  ALWAYS use dates in their natural data types for sorting or comparisons.

Author

Commented:
Pathartman

Thank you for that info

Alan

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial