Reputation: 5341
Ok I cant get my head round this, ive looked at so many posts on SF but cant figure it out.
I need to compare two dates & times, start and end. If end is great then alert();
Works in Chrome but not IE(9) (format is: 01-Jan-2013 10:00)
var stDate = new Date(date +" "+ start);
var enDate = new Date(dateEnd + " "+ end);
if ( Date.parse ( enDate ) > Date.parse ( stDate ) ) {
alert('on no');
}
Please help, im stuck...
Upvotes: 2
Views: 6605
Reputation: 3700
Just make a custom parser, it's done faster than trying to figure how different browsers treat various time string formats:
function parse(datestring){
var months = {"Jan":0,"Feb":1,"Mar":2,"Apr":3,"May":4,"Jun":5,"Jul":6,"Aug":7,"Sep":8,"Oct":9,"Nov":10,"Dec":11}
var timearray = datestring.split(/[\-\ \:]/g)
return Date.UTC(timearray[2],months[timearray[1]],timearray[0],timearray[3],timearray[4])
}
This returns Unix time in milliseconds, and it use UTC, thus avoiding complications from the missing hour of daylight savings time. It works with the format you specified, but does not validate input.
Upvotes: 3
Reputation: 842
var stDate = new Date(date +" "+ start);
var enDate = new Date(dateEnd + " "+ end);
if ( enDate.getTime() > stDate.getTime() ) {
alert('on no');
}
To create a Date object:
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
getTime() Return the number of milliseconds since 1970/01/01
Upvotes: 0
Reputation: 47099
if ( enDate.getTime() > stDate.getTime() ) {
alert('oh no');
}
Pushing to number (+enDate
) is the same as using the .getTime()
method:
if ( +enDate > +stDate ) {
alert('oh no');
}
Upvotes: 2