旅游日期无效 [英] Invalid date in safari
问题描述
alert(new Date('2010-11-29'));
chrome,ff没有这个问题,但safari会发出无效的日期。为什么?
chrome, ff doesn't have problems with this, but safari cries "invalid date". Why ?
编辑:好的,根据下面的评论,我使用字符串解析,并尝试这样:
edit : ok, as per the comments below, I used string parsing and tried this :
alert(new Date('11-29-2010')); //doesn't work in safari
alert(new Date('29-11-2010')); //doesn't work in safari
alert(new Date('2010-29-11')); //doesn't work in safari
推荐答案
code> yyyy-MM-dd 不是 Date
构造函数的正式支持格式。 Firefox似乎支持它,但不要依赖于其他浏览器执行相同操作。
The pattern yyyy-MM-dd
isn't an officially supported format for Date
constructor. Firefox seems to support it, but don't count on other browsers doing the same.
以下是一些支持的字符串,取自这个网站:
Here are some of supported strings, taken from this site:
- MM-dd-yyyy
- yyyy / MM / dd
- MM / dd / yyyy
- MMMM dd,yyyy
- MMM dd,yyyy
- MM-dd-yyyy
- yyyy/MM/dd
- MM/dd/yyyy
- MMMM dd, yyyy
- MMM dd, yyyy
DateJS 似乎是解析非标准日期格式的好库。
DateJS seems like a good library for parsing non standard date formats.
编辑:只是检查 ECMA-262标准。来自第15.9.1.15节:
Edit: just checked ECMA-262 standard. Quoting from section 15.9.1.15:
日期时间字符串格式
ECMAScript定义基于简化ISO
8601扩展格式的日期时间
的字符串
交换格式。格式为
,如下所示:YYYY-MM-DDTHH:mm:ss.sssZ
其中的字段如下:
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ Where the fields are as follows:
- YYYY是公历中年份的十进制数字。
- :(hyphon)在字符串中直接显示两次。
- MM是从01(1月)到12(12月)的一年中的月份。
- DD是01到31月的日期。
- TT字面上出现在字符串中,表示
时间元素。 - HH是自午夜以来的两个
十进制数字的完成小时数。 - ::(冒号)字符串中两次出现。
- mm是从小时开始的完成分钟数,
为两位十进制数字。 - ss是从分钟开始的完整秒数
作为两位十进制数字。 - 。 。 (点)字面上出现在字符串中。
- sss是从
秒开始以三位十进制数后的完整毫秒数。两个
的。并且可以省略毫秒字段
。 - Z是指定为Z(UTC)或+或 -
的时区偏移量,后跟时间表达式hh:mm
- YYYY is the decimal digits of the year in the Gregorian calendar.
- ":" (hyphon) appears literally twice in the string.
- MM is the month of the year from 01 (January) to 12 (December).
- DD is the day of the month from 01 to 31.
- T "T" appears literally in the string, to indicate the beginning of the time element.
- HH is the number of complete hours that have passed since midnight as two decimal digits.
- : ":" (colon) appears literally twice in the string.
- mm is the number of complete minutes since the start of the hour as two decimal digits.
- ss is the number of complete seconds since the start of the minute as two decimal digits.
- . "." (dot) appears literally in the string.
- sss is the number of complete milliseconds since the start of the second as three decimal digits. Both the "." and the milliseconds field may be omitted.
- Z is the time zone offset specified as "Z" (for UTC) or either "+" or "-" followed by a time expression hh:mm
此格式包括日期格式:
- YYYY
- YYYY-MM
- YYYY-MM-DD
它还包括带有
的仅限时间表格,附加可选的时区偏移量:
It also includes time-only forms with an optional time zone offset appended:
- THH:mm
-
- THH:mm:ss
- THH:mm:ss.sss
还包括日期时间,其中
可以是上述的任何组合。
Also included are "date-times" which may be any combination of the above.
所以,似乎YYYY-MM-DD被包含在标准中,但由于某些原因,Safari不支持它。
So, it seems that YYYY-MM-DD is included in the standard, but for some reason, Safari doesn't support it.
更新 :在查看 datejs文档后,使用它,您的问题应该解决使用代码喜e:
Update: after looking at datejs documentation, using it, your problem should be solved using code like this:
var myDate1 = Date.parseExact("29-11-2010", "dd-MM-yyyy");
var myDate2 = Date.parseExact("11-29-2010", "MM-dd-yyyy");
var myDate3 = Date.parseExact("2010-11-29", "yyyy-MM-dd");
var myDate4 = Date.parseExact("2010-29-11", "yyyy-dd-MM");
这篇关于旅游日期无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!