今天在测试的时候发现,在Chrome中的如下代码:

new Date("2014-03-09");

在Safari中报错invalid date。经过查阅资料找到类似的问答:

stackOverflow地址:http://stackoverflow.com/questions/4310953/invalid-date-in-safari

解释与翻译如下:

目前Safari可以支持的标准格式如下:

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

DateJS 是一个很好的格式化非标准格式日期的库。

关于Date日期标准,原文截取ECMA-262 standard内容进行说明,引文如下:

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:

ECMAScript为基于ISO 8601扩展格式的日期时间定义了一个字符串交换格式。格式为YYYY-MM-DDTHH:mm:ss.sssZ,每个域的介绍如下:

  • YYYY is the decimal digits of the year in the Gregorian calendar. YYYY为格林威治时间年的十进制表达
  • ":" (hyphon) appears literally twice in the string. ":"字面上出现在两个字符串之间
  • MM is the month of the year from 01 (January) to 12 (December). MM表示月份从01(一月)到12(十二月)
  • DD is the day of the month from 01 to 31. DD表示月份中的天数从01到31.
  • T "T" appears literally in the string, to indicate the beginning of the time element. T字面上出现在字符串中,表明时间元素的开始
  • HH is the number of complete hours that have passed since midnight as two decimal digits. HH表示从午夜算起,已经经过的完整两位小时数字
  • : ":" (colon) appears literally twice in the string. ":"字面上出现在两个字符串之间
  • mm is the number of complete minutes since the start of the hour as two decimal digits. mm表示从一个小时的开始算起,已经经过的完整两位分钟数字
  • ss is the number of complete seconds since the start of the minute as two decimal digits. ss表示从一分钟的开始算起,已经经过的完整两位秒数数字
  • . "." (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.  sss表示从一秒钟的开始算起,已经经过的完整毫秒数,用三位数表示。该域可忽略不写。
  • Z is the time zone offset specified as "Z" (for UTC) or either "+" or "-" followed by a time expression hh:mm Z特指时区偏移(特指UTC)或使用跟随有时间表达式hh:mm 的"+"、"-"。如 +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. 同时也可以包含以上提到的日期或时间的组合。

所以可以看到,问题在于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");

不过,如果仅是很少量的使用日期时间,个人认为无需打动干戈的去使用DateJS这种库,简单的进行正则匹配为safari可以识别的格式即可,如下:

new Date('2011-04-12'.replace(/-/g, "/"))

更多其他的变化,可以根据自己的业务需求进行代码上的调整。

Safari中的new Date()格式化坑的更多相关文章

  1. 日期转换时Safari中返回Invalid Date

    问题: 进行日期转换时,Safari中会返回Invalid Date, 而IE 9, Firefox, Chrome and Opera显示正常,代码如下所示: var d = new Date(&q ...

  2. Safari中的Date

    在js中处理Date时,发现Safari和其他浏览器的支持方式不一致 1.例如:2017-01-01 12:00:00 在其他浏览器中,使用这个格式的字符串进行new Date操作没有问题,但是在Sa ...

  3. Javascript中new Date的坑

    在一段判断是否过期的js代码中是这么写的: if (new Date() < new Date(2014, 9, 25)) { //... } 后来发现过了9月25日竟然不过期,console. ...

  4. javascript 的Date 格式化, 模仿shell中date命令的格式

    原文:javascript 的Date 格式化, 模仿shell中date命令的格式 shell 中显示当前的日期 [root@localhost]$ date '+%Y-%m-%d %H:%M:%S ...

  5. JS Date函数在safari中的问题

    问题描述:在做Web的时候,在PC上用Chrome调试成功,但是在safari一测就出现了问题.经过debug发现是日期相关出现问题.查阅一些资料后发现,safari中对于JavaScript的Dat ...

  6. Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例

    Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...

  7. PHP date 格式化一个本地时间/日期

    PHP date 格式化一个本地时间/日期 date (PHP 4, PHP 5) date — 格式化一个本地时间/日期 说明 string date ( string $format [, int ...

  8. ExtJs 中的ext.date

    获取当前日期直接用NEW date() Ext.Date英文API http://docs.sencha.com/ext-js/4-1/#!/api/Ext.Date Ext.Date是一个单例类,封 ...

  9. php 中时间函数date及常用的时间计算

    曾在项目中需要使用到今天,昨天,本周,本月,本季度,今年,上周上月,上季度等等时间戳,趁最近时间比较充足,因此计划对php的相关时间知识点进行总结学习 1,阅读php手册date函数 常用时间函数: ...

随机推荐

  1. iOS开发-定制多样式二维码

    iOS开发-定制多样式二维码   二维码/条形码是按照某种特定的几何图形按一定规律在平台(一维/二维方向上)分布的黑白相间的图形纪录符号信息.使用若干个与二进制对应的几何形体来表示文字数值信息. 最常 ...

  2. winform对话框控件

    (1)ColorDialog     用户自定义颜色控件 点击颜色按键,改变richTextBox1中字体的颜色 private void button1_Click(object sender, E ...

  3. IO字 节流/字符流 读取/写入文件

    流是指一连串流动的数据信号,以先进,先出的方式发送和接收的通道 流的分类根据方向分为输入流所有接收,获得,读取的操作都是属于输入流所有的输入流名字都带有input或Reader 输出流所有发送,写的操 ...

  4. IE 9 以下兼容HTML5

    <head> <meta name="viewport" content="width=device-width,initial-scale=1.0&q ...

  5. MVC Model数据验证

    概述 上节我们学习了Model的数据在界面之间的传递,但是很多时候,我们在数据传递的时候为了确保数据的有效性,不得不给Model的相关属性做基本的数据验证. 本节我们就学习如何使用 System.Co ...

  6. DateUtils

    package com.vcredit.ddcash.batch.util; import java.text.SimpleDateFormat;import java.util.Calendar;i ...

  7. windows下vim 块模式问题

    VIM: gvim 使用 Ctrl+V 發表於 2005 年 10 月 27 日 由 Tsung vim 要做垂直選取的動作, 就要使用 "Ctrl + v", 但是 gvim 會 ...

  8. centos安装php-memcached扩展及cas用法

    一.安装libmemcachedwget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar ...

  9. 【转】Unity中的协同程序-使用Promise进行封装(三)

    原文:http://gad.qq.com/program/translateview/7170967 译者:崔国军(飞扬971)    审校:王磊(未来的未来) 在这个系列的最后一部分文章,我们要通过 ...

  10. Array与ArrayBuffer

    一.概述 二:Array使用 1.建立方式三种(数组里可以有不同的数据类型) 2.Array的简单使用 三:ArrayBuffer的简单使用 1.ArrayBuffer建立 导包 2.添加元素(+=) ...