Date function not working in IE8

2019-01-28 22:40发布

I've got the following which works fine in Chrome:

function funLoad(str1,str3,str4)
{

    var dym1 = str1.split("/");

    var d=new Date();
    var dym2 = d.getMonth() + 1 + " " + d.getDate() + " " + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":00";
    //var dym2 = "6 10 2013 09:00:00";

    var start = Date.parse(dym1[1] + " " + dym1[0] + " " + dym1[2] + " " + str3 + ":" + str4 + ":00"); 
    var end = Date.parse(dym2);

    return (start-end) / (1000*60*60);

}

$("#btn1").click(function(event){
    alert(funLoad($("#txt1").val(),$("#ddlHourTime").val(),$("#ddlMinuteTime").val()));
});

Here is a jsFiddle: http://jsfiddle.net/oshirowanen/QTVWd/6/

When I run this in IE8, I just get alerted with NaN.

2条回答
相关推荐>>
2楼-- · 2019-01-28 23:23

To simplify a little bit the error is because the format used by IE to store dates is not the same used by, for example, Chrome (and the one you're using to manually parse and format the date). It's allowed by the standard, what is required is the browser can parse the format produced by itself (see §15.9.4.2).

Usually to work directly with date format is not a good idea, not only because browser specific implementation but because of globalization (this is especially true for a web application with a virtual world wide audience). In practice what I mean is DO NOT EVER DO something like this (in this post I try to explain the reasons):

d.getMonth() + 1 + " " + d.getDate() + " " + d.getFullYear()

or this:

d.getMonth() + 1 + "-" + d.getDate() + "-" + d.getFullYear()

Few exceptions to this rule:

  • You're formatting that string for end-user (and only if you don't care about her date format, please note that with that code you're assuming MDY order and a specific separator).
  • You'll use that string by yourself and you'll parse it with another handmade parser (you won't use Date.parse()) because it's locale and browser dependent.
  • You're writing your own library to manage dates.

The only format you're sure every browser (that supports ECMAScript 5) will read is ISO 8601 YYYY-MM-DDTHH:mm:ss.sssZ (see §15.9.1.15) so in your case you should change your custom parsing/formatting to that. For older browser there is not a clear rule (that's why we need a library). The standard says at §15.9.4.2 that:

If the String does not conform to that format [ISO 8601] the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

(emphasis is mine)

Take a look to this and this posts on SO for other details (or this little tutorial about dates).

What I suggest, if you work with dates across browsers and locales, is to use a good library to abstract all this details. I found this one is pretty strong and easy to use. If you want a widely used almost complete library take a look to moment.js too.

查看更多
Ridiculous、
3楼-- · 2019-01-28 23:34

Looks like that format is not supported in IE

var dym2 = d.getMonth() + 1 + "-" + d.getDate() + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":00";
查看更多
登录 后发表回答