javascript date() 函数在 Safari 和 Firefox 中返回无效日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16616950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
date() function returning Invalid Date in Safari and Firefox
提问by Ben Pearce
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());
When I run this in Chrome it outputs:
当我在 Chrome 中运行它时,它输出:
Sun May 12 2013 00:00:00 GMT-0700 (PDT)
Which is what I need, however when I run this in Firefox or Safari I get
这是我需要的,但是当我在 Firefox 或 Safari 中运行它时,我得到
Invalid Date
Can any one suggest a workaround for this. Extra points if it doesn't require a library, regex or string manipulation.
任何人都可以为此提出一种解决方法。如果不需要库、正则表达式或字符串操作,则额外加分。
回答by Matt Johnson-Pint
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.
虽然该值2013-05-12 20:00:00
是ISO8601指定的几种有效格式之一,但最好使用RFC3339 中定义的配置文件,它是 ISO8601 的子集。
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
.
这意味着您应该同时拥有T
分隔符和时区偏移量 - 可以是数字或Z
指定 UTC。换句话说,您应该使用像2013-05-12T20:00:00-07:00
或等效的值2013-05-13T03:00:00Z
。
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.
现在无论您是否有正确格式化的输入值,都会出现浏览器兼容性问题。所有现代浏览器都支持 ISO8601,但有些浏览器对它的解释不同。例如,在谷歌浏览器中,如果省略T
它,即使Z
末尾有一个,它也会被解析为本地时间。在 Internet Explorer 中,T
省略的相同值返回无效日期错误。
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。除了完全支持 ISO 日期之外,您还可以使用自定义格式字符串以任何方式解析输入。它还有许多其他功能。我强烈建议你看一看。
Using moment.js, you can do the following:
使用moment.js,您可以执行以下操作:
// 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"
As far as I know, moment.jsworks in all browsers - including older ones like IE6.
据我所知,moment.js适用于所有浏览器——包括像 IE6 这样的旧浏览器。
回答by Vadim
Your date format is not standard for some browsers. To parse custom date time formats you can split it to separate values and use another Date
constructor that accepts year, month, day, hours, minutes, seconds
and is cross browser compliant. For your case it will be something like this:
您的日期格式不是某些浏览器的标准格式。要解析自定义日期时间格式,您可以将其拆分为单独的值,并使用另一个Date
接受year, month, day, hours, minutes, seconds
且跨浏览器兼容的构造函数。对于您的情况,它将是这样的:
var date = "2013-05-12 20:00:00",
values = date.split(/[^0-9]/),
year = parseInt(values[0], 10),
month = parseInt(values[1], 10) - 1, // Month is zero based, so subtract 1
day = parseInt(values[2], 10),
hours = parseInt(values[3], 10),
minutes = parseInt(values[4], 10),
seconds = parseInt(values[5], 10),
formattedDate;
formattedDate = new Date(year, month, day, hours, minutes, seconds);
Working example here http://jsbin.com/ewozew/1/edit
回答by Sam Granger
I ran into this exact same issue today, do not use dashes as a seperator. This works fine in Chrome but not in Safari/iOS, replacing dashes with slashes fixes this issue. The Date function will otherwise break in Safari.
我今天遇到了这个完全相同的问题,不要使用破折号作为分隔符。这在 Chrome 中工作正常,但在 Safari/iOS 中无效,用斜杠替换破折号可解决此问题。否则,日期函数将在 Safari 中中断。
Incorrect date string with with -
不正确的日期字符串与 -
The following code will return the following
以下代码将返回以下内容
new Date('03-25-2017');
Chrome:
铬合金:
Sat Mar 25 2017 00:00:00 GMT+0100 (CET)
Safari:
苹果浏览器:
Invalid Date
So, simply replace -with /.
因此,只需将-替换为/。
Correct date string with with /
使用 / 更正日期字符串
The following code will return the following
以下代码将返回以下内容
new Date('03/25/2017');
Chrome:
铬:
Sat Mar 25 2017 00:00:00 GMT+0100 (CET)
Safari:
野生动物园:
Sat Mar 25 2017 00:00:00 GMT+0100 (CET)
回答by Rob Johnstone
I think you need a 'T' between the date and the time to get it working fully.
我认为您需要在日期和时间之间添加一个“T”才能使其完全正常工作。
date = new Date("2013-05-12T20:00:00");
There are quite a few possible datetime formats allowed in the spec and I don't think all browsers offer all possibilities. For Firefox see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse
规范中允许使用多种可能的日期时间格式,我认为并非所有浏览器都提供所有可能性。对于 Firefox,请参阅https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse
To be really safe cross-browser you might want to try the RFC2822 / IETF date syntax (basically the same as Chrome spat back out at you)
为了真正安全的跨浏览器,您可能想尝试使用 RFC2822 / IETF 日期语法(与 Chrome 向您吐口水基本相同)