safari 日期对象新建new Date( timeStr ) 参数TimeStr格式

  这是一个浏览器兼容的问题,在此总结一下,别老在这掉坑。

  先坐下测试

 

var timeStrArray = [
        '2016-10-04',
        '2016.10.04',
        '2016/10/04',

        '10-04-2016',
        '10.04.2016',
        '10/04/2016',
    ];
    for (var i = 0,len = timeStrArray.length; i < len; i++) {
        var timeStr = timeStrArray[i];
        console.log(new Date(timeStr));
    }

  在Chrome中的结果

Tue Oct 04 2016 00:00:00 GMT+0800 (中国标准时间)
Tue Oct 04 2016 08:00:00 GMT+0800 (中国标准时间)
Tue Oct 04 2016 00:00:00 GMT+0800 (中国标准时间)
Tue Oct 04 2016 00:00:00 GMT+0800 (中国标准时间)
Tue Oct 04 2016 00:00:00 GMT+0800 (中国标准时间)
Tue Oct 04 2016 00:00:00 GMT+0800 (中国标准时间)

在safari中的结果

Invalid Date
Invalid Date
Tue Oct 04 2016 00:00:00 GMT+0800 
Invalid Date
Invalid Date
Tue Oct 04 2016 00:00:00 GMT+0800 

可以看到只有以‘/’分隔的字符串能真确的生成日期对象。

这是只有 年 月 日 三个日期的字符串还有包含时间的字符串

var timeStrArray = [
    // 加上时间
    '2016-10-04 20:09:23',
    '2016.10.04 20:09:23',
    '2016/10/04 20:09:23',

    '10-04-2016 20:09:23',
    '10.04.2016 20:09:23',
    '10/04/2016 20:09:23',
];

结果同上

  

关于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:

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 This format includes date-only forms:  YYYY YYYY-MM YYYY-MM-DDIt 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. 

 

转载于:https://www.cnblogs.com/pipu-qiao/p/5946891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值