zoukankan      html  css  js  c++  java
  • 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. 

  • 相关阅读:
    Ceres求解直接法BA实现自动求导
    栈溢出漏洞原理详解与利用
    【机器学习】使用CNN神经网络实现对图片分类识别及模型转换
    【Android】java中调用JS的方法
    5 项大奖,70 项满分!阿里云全方位引领云原生技术升级
    分布式系统架构与云原生—阿里云《云原生架构白皮书》导读
    Dubbo-go 发布 1.5 版,朝云原生迈出关键一步
    阿里产品专家:高情商的技术人,如何做沟通?
    CNCF 新增两个孵化项目 | 云原生生态周报 Vol. 58
    掌门教育微服务体系 Solar | 阿里巴巴 Nacos 企业级落地上篇
  • 原文地址:https://www.cnblogs.com/pipu-qiao/p/5946891.html
Copyright © 2011-2022 走看看