Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] Compare two dates


  • From: nick at nickfitz.co.uk (Nick Fitzsimons)
  • Subject: [Javascript] Compare two dates
  • Date: Fri Oct 14 09:46:38 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/