Compare Date & Time Javascript
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(); Wo
Solution 1:
Just make a custom parser, it's done faster than trying to figure how different browsers treat various time string formats:
functionparse(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)
returnDate.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.
Solution 2:
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');
}
Solution 3:
var stDate = newDate(date +" "+ start);
var enDate = newDate(dateEnd + " "+ end);
if ( enDate.getTime() > stDate.getTime() ) {
alert('on no');
}
To create a Date object:
var d = newDate();
var d = newDate(milliseconds);
var d = newDate(dateString);
var d = newDate(year, month, day, hours, minutes, seconds, milliseconds);
getTime() Return the number of milliseconds since 1970/01/01
Post a Comment for "Compare Date & Time Javascript"