Grezly
Grezly

Reputation: 498

Date comparison javascript

Is string comparison on the next manner reliable? In other words, how does this comparison internally works?

var _date = new Date();
var _start_date = _date.getFullYear()+"-"+_date.getMonth()+"-"+_date.getDate()+"-"+_date.getHours+"-"+_date.getMinutes();

if( _start_date < "2011-1-11-23-59")
    alert('still before the 11th of january!');

The above works perfectly, but how is javascript comparison works this way? I know i can calculate based on EPOCH and or new Date(), but in my case that isn't possible.

Upvotes: 0

Views: 568

Answers (1)

Hogan
Hogan

Reputation: 70513

This seems better to me:

  if (new Date() < new Date("January 11, 2011 00:00:00"))
     alert('still before the 11th of january!');

See

http://jsfiddle.net/uM3Mv/1/

and

http://jsfiddle.net/uM3Mv/2/

Upvotes: 1

Related Questions