2

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...

D-W
  • 5,201
  • 14
  • 47
  • 74

3 Answers3

3

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.

aaaaaaaaaaaa
  • 3,630
  • 1
  • 24
  • 23
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');
}
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0
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

HMarioD
  • 842
  • 10
  • 18
  • @D-W getTime() works for all browsers. http://www.w3schools.com/jsref/jsref_settime.asp – HMarioD Jan 11 '13 at 21:23
  • May be you have some problem with your variables(date, start, dateEnd or end) try create dates like that new Date("2013/01/03 12:30"); and new Date("2013/01/03 12:31"); and see if working. – HMarioD Jan 11 '13 at 21:29