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. 

  • 相关阅读:
    datalist标签
    meter标签
    audio标签
    video标签
    time标签
    figure标签
    正则收集
    js文字无缝滚动
    页面滚动到指定位置
    Css公共文件结构
  • 原文地址:https://www.cnblogs.com/pipu-qiao/p/5946891.html
Copyright © 2011-2022 走看看