Safari 中的日期无效 [英] Invalid date in safari

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

问题描述

 alert(new Date('2010-11-29'));

chrome, ff 对此没有问题,但 safari 会喊无效日期".为什么?

好的,根据下面的评论,我使用了字符串解析并尝试了这个:

alert(new Date('11-29-2010'));//在 safari 中不起作用警报(新日期('29-11-2010'));//在 safari 中不起作用警报(新日期('2010-29-11'));//在 safari 中不起作用

edit Mar 22 2018 :似乎人们仍然在这里登陆 - 今天,我会使用 momentdate-fns 并完成用它.Date-fns 也非常无痛且轻便.

解决方案

模式 yyyy-MM-dd 不是 Date 构造函数的官方支持格式.Firefox 似乎支持它,但不要指望其他浏览器也会这样做.

以下是一些支持的字符串:

  • MM-dd-yyyy
  • yyyy/MM/dd
  • MM/dd/yyyy
  • MMMM dd, yyyy
  • MMM dd, yyyy

DateJS 似乎是解析非标准日期格式的好库.

编辑:刚刚检查了 ECMA-262标准.引自第 15.9.1.15 节:

日期时间字符串格式

<块引用>

ECMAScript 定义了一个字符串日期时间的交换格式基于 ISO 的简化8601 扩展格式.格式是如下: YYYY-MM-DDTHH:mm:ss.sssZ其中字段如下:

  • YYYY 是公历中年份的十进制数字.
  • "-"(hyphon) 在字符串中出现两次.
  • MM 是一年中从 01(一月)到 12(十二月)的月份.
  • DD 是从 01 到 31 的月份中的第几天.
  • T"字面出现在字符串中,表示开始时间元素.
  • HH 是自午夜以来经过的完整小时数为 2十进制数字.
  • ":"(冒号)在字符串中出现两次.
  • mm 是自小时开始以来的完整分钟数,如两位十进制数字.
  • ss 是自分钟开始以来的完整秒数两位十进制数字.
  • "."(点)字面出现在字符串中.
  • sss 是自开始以来的完整毫秒数第二个是三位十进制数字.两个都."并且毫秒字段可能被省略.
  • Z 是指定为Z"的时区偏移量;(对于UTC)或+"或-"后跟时间表达式 hh:mm

此格式包括仅日期格式:

  • YYYY
  • YYYY-MM
  • YYYY-MM-DD

<块引用>

它还包括具有时间限制的表单附加的可选时区偏移量:

  • THH:mm
  • THH:mm:ss
  • THH:mm:ss.sss

还包括日期时间"哪一个可以是以上任意组合.

所以,YYYY-MM-DD 似乎包含在标准中,但出于某种原因,Safari 不支持它.

更新:查看datejs 文档,使用它,您的问题应该使用如下代码解决:

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");

 alert(new Date('2010-11-29'));

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

edit Mar 22 2018 : Seems like people are still landing here - Today, I would use moment or date-fns and be done with it. Date-fns is very much pain free and light as well.

解决方案

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 supported strings:

  • MM-dd-yyyy
  • yyyy/MM/dd
  • MM/dd/yyyy
  • MMMM dd, yyyy
  • MMM dd, yyyy

DateJS seems like a good library for parsing non standard date formats.

Edit: just checked ECMA-262 standard. Quoting from section 15.9.1.15:

Date Time String Format

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 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" 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

This format includes date-only forms:

  • 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.

So, it seems that YYYY-MM-DD is included in the standard, but for some reason, Safari doesn't support it.

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");

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

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