Javascript Date.parse help

前端 未结 3 1742
孤独总比滥情好
孤独总比滥情好 2021-01-19 23:42

I am using Javascript Date.parse() to check if a start time is after an end time.

The time in question is like this:

Date.parse("12:0         


        
相关标签:
3条回答
  • 2021-01-20 00:08

    What version of IE are you testing this on? IE8 correctly returns false for me.

    0 讨论(0)
  • 2021-01-20 00:17

    You can not just pass a time to Date.parse(), as it is expecting a datestring. If you flip the > in your code to a <, you'll notice it still returns false. This is because Date.parse() is returning NaN.

    Try this:

    var a = new Date("January 1, 1970 12:00:00");
    var b = new Date("January 1, 1970 21:30:00");
    
    if (a > b) { alert(true); } else { alert(false); }
    if (a < b) { alert(true); } else { alert(false); }
    
    0 讨论(0)
  • 2021-01-20 00:21

    The docs for Date.parse() in IE state the following:

    If the 24-hour clock is used, it is an error to specify "PM" for times later than 12 noon. For example, "23:15 PM" is an error.

    For a cross-browser solution, you should avoid parse() and parse the time string manually. Alternatively, you could use a cross-browser library for parsing dates/times - DateJS is a popular one.

    0 讨论(0)
提交回复
热议问题