[Javascript] Compare two dates

Peter-Paul Koch pp.koch at gmail.com
Fri Oct 14 10:26:54 CDT 2005


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

By far the easiest way: make both dates milliseconds and compare them.
Milliseconds are the only simple way of comparing dates.

var date1 = new Date(whatever);
var date2 = new Date(whatever);

var date1Comp = date1.getTime(); // milliseconds
var date2Comp = date2.getTime();

if (date1Comp < date2Comp)
{
  // date 1 is earlier than date 2
}

--
-------------------------------------------------------------------
ppk, freelance web developer
http://www.quirksmode.org/
------------------------------------------------------------------



More information about the Javascript mailing list