[Javascript] Compare two dates

Nick Fitzsimons nick at nickfitz.co.uk
Fri Oct 14 09:42:00 CDT 2005


> Hello everybody,
>
> i would like to validate/compare two dates. The first date should be
> earlier
> than the second one.
>

Dates are represented internally as a 32-bit number, so

if (firstDate < secondDate) {
   // first date is earlier than second date
} else {
   // first date is equal to or later than second date
}

Note that this works to millisecond precision, so two dates that are the
same will be different if their times are different. In other words,

2005-10-15 15:00:00

is less than

2005-10-15 15:00:01

You can work around this, if necessary, by explicitly setting the times of
the Date objects, using setHour etcetera.

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/



More information about the Javascript mailing list