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

  • 相关阅读:
    成为一个优秀的C++程序员
    C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast
    C++经典类库(C++开发必看)
    auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解
    C++ clone()函数的用法
    c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
    C++模板详解(转)
    static变量的作用(转)
    2018年12月17日,新工作,可是一点提不起兴趣,在原点转啊转,园子什么时候会关了呢,我们终将老去
    给C#Control组件统一增加加属性
  • 原文地址:https://www.cnblogs.com/pipu-qiao/p/5946891.html
Copyright © 2020-2023  润新知