Chrome无法解析日期,但IE可以 [英] Chrome can't parse date but IE can

查看:208
本文介绍了Chrome无法解析日期,但IE可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解析我的网站的日期。它曾经在IE和Chrome上工作正常(我还没有尝试过Firefox)。但是最近我一直无法解析chrome的日期。

I'm having trouble parsing dates for my website. It used to work fine on both IE and Chrome (I haven't tried Firefox yet). But recently I have been unable to parse dates on chrome.

从数据库中获取的日期字符串看起来像这样Oct 17 2016 12:00 AM

The date string which I obtain from the database looks like this "Oct 17 2016 12:00AM"

这是我用来调试我的问题的代码

Here is the code I'm using to debug my problem

console.log("String = "+item.DueDate);
console.log("new Date = " + new Date(item.DueDate));
console.log("Date Parse = " + Date.parse(item.DueDate));
console.log("Moment Parse = " + moment(item.DueDate));

这是IE返回的内容:

String = Oct 17 2016 12:00 AM

new Date = Mon Oct 17 2016 00:00:00 GMT-0400(Eastern Daylight Time)

Date Parse = 1476676800000

Moment Parse = 1476676800000

String = Oct 17 2016 12:00AM
new Date = Mon Oct 17 2016 00:00:00 GMT-0400 (Eastern Daylight Time)
Date Parse = 1476676800000
Moment Parse = 1476676800000

这里是Chrome返回的内容: p>

And here is what is returned by Chrome:


String = Oct 17 2016 12:00 AM

新日期=无效日期

日期解析= NaN

时刻分析= NaN

String = Oct 17 2016 12:00AM
new Date = Invalid Date
Date Parse = NaN
Moment Parse = NaN

我在我的一个函数中使用Date.parse()与日期之间的差异:

I use Date.parse() in one of my function that finds the difference between to dates:

function daydiff(first, second) {
    return Math.round((second - first) / (1000 * 60 * 60 * 24));
}

var dif = daydiff(Date.parse(item.DueDate), Date.parse(item.DateShipped));

我的日期字符串应该怎么做,以便它与Chrome和Internet Explorer兼容?

What should I do to my date string in order for it to work with both chrome and internet explorer?

修正

所以我通过更改我的web api调用来修复它一个DateTime而不是字符串。

So I fixed it by changing my web api call to return a DateTime rather than string.

推荐答案

不要使用Date构造函数解析字符串(或者Date.parse,它们等同于解析)因为它几乎完全取决于实现。即使在ECMA-262中指定的格式也不能被所有正在使用的浏览器进行可靠的解析。

Never parse strings with the Date constructor (or Date.parse, they are equivalent for parsing) as it is almost entirely implementation dependent. Even the one format specified in ECMA-262 is not reliably parsed by all browsers in use.

使用定制函数或提供解析和格式化的库,并始终将格式传递给解析器。合适的图书馆是 moment.js date.js fecha .js ,但还有其他许多。

Use a bespoke function or a library that provides parsing and formatting and always pass the format to parse to the parser. Suitable libraries are moment.js, date.js and fecha.js, but there are many others.

定制功能可能如下所示:

A bespoke function might look like:

function parseSpecia(s) {
    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 h;
    if (/a[mp]$/i.test(s)) {
        h = /am$/i.test(s)? 0 : 12;
        s = s.replace(/a[mp]$/i,'');
    }
    var b = s.split(/[ :]/)
    return new Date(b[2], months[b[0].toLowerCase().substr(0,3)], b[1],(b[3]%12)+h, b[4]);
}

var s = 'Oct 17 2016 12:00AM';
console.log(parseSpecia(s));

而使用图书馆的样子如下所示:

Whereas using a library would look like:

fecha.parse('Oct 17 2016 12:00AM','MMM DD YYYY hh:mm:zz');

这篇关于Chrome无法解析日期,但IE可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆