date()函数在Safari和Firefox中返回无效日期 [英] date() function returning Invalid Date in Safari and Firefox

查看:396
本文介绍了date()函数在Safari和Firefox中返回无效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以下列方式格式化日期:

I am formatting a date in the following way:

date = new Date("2013-05-12 20:00:00");
formattedDate = new Date(date.getFullYear(),date.getMonth(),date.getDate());

当我在Chrome中运行时输出:

When I run this in Chrome it outputs:

Sun May 12 2013 00:00:00 GMT-0700 (PDT)

我需要什么,但是当我在Firefox或Safari中运行时,我得到

Which is what I need, however when I run this in Firefox or Safari I get

Invalid Date

任何人都可以提出解决方法。额外的点,如果不需要一个库,正则表达式或字符串操作。

Can any one suggest a workaround for this. Extra points if it doesn't require a library, regex or string manipulation.

推荐答案

虽然值 2013-05-12 20:00:00 ISO8601指定的几种有效格式,最好使用 RFC3339 中定义的配置文件,该配置文件是

While the value 2013-05-12 20:00:00 is one several valid formats specified by ISO8601, it is best to use the profile defined in RFC3339, which is a sub-set of ISO8601.

这意味着您应该同时拥有 T 分隔符和时区偏移 - 可以是数字或 Z 来指定UTC。换句话说,你应该使用如$ code> 2013-05-12T20:00:00-07:00 或相当于 2013-05- 13T03:00:00Z

This means that you should have both the T separator, and a time zone offset - which can be numeric or Z to specify UTC. In other words, you should use a value like 2013-05-12T20:00:00-07:00, or the equivalent 2013-05-13T03:00:00Z.

现在无论您是否输入了正确格式的输入值,都会出现浏览器兼容性问题。所有现代浏览器都支持ISO8601,但有些解释不同。例如,在Google Chrome中,如果您省略了 T ,则将其解析为本地时间,即使有 Z 最后。在Internet Explorer中,与 T 省略的相同值返回无效的日期错误。

Now whether you have a correctly formatted input value or not, then the issue of browser compatibility comes up. All modern browsers support ISO8601, but some interpret it differently. For example, in Google Chrome, if you omit the T it is parsed as local time even if there is a Z at the end. In Internet Explorer, the same value with the T omitted returns an invalid date error.

更好的方法是使用一个可以将这些差异抽象出来的库,因此您可以专注于应用程序,而不是浏览器的怪癖。我知道的最好的图书馆是 moment.js 。除了完全支持ISO日期,您可以使用自定义格式字符串以任何方式解析输入。它还有许多其他功能。我非常鼓励你去看看。

A better approach is to use a library that abstracts these differences away, so you can focus on your application and not the quirks of the browser. The best library I know of for this is moment.js. In addition to full support for ISO dates, you can parse input in any way that you want with custom format strings. It has lots of other features as well. I highly encourage you to take a look.

使用 moment.js ,您可以执行以下操作:

Using moment.js, you can do the following:

// from a full date-time-offset ISO8601/RFC3339 value
var m = moment('2013-05-12T20:00:00-07:00');

// or from your custom value, with explicit formatting
// this will assume the local time zone because you didn't specify one
var m = moment('2013-05-12 20:00:00','YYYY-MM-DD HH:mm:ss');

// then you can output it in a custom format if you like
m.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, May 12th 2013, 8:00:00 pm"

据我所知, moment.js 适用于所有浏览器,包括较旧的浏览器,如IE6。

As far as I know, moment.js works in all browsers - including older ones like IE6.

这篇关于date()函数在Safari和Firefox中返回无效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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