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. 

  • 相关阅读:
    最大匹配之匈牙利算法模板。。 (转)
    hdu 1597 find the nth digit
    hdu 1258 Sum It Up ( dfs )
    hdu 1596 find the safest road(Dijkstra)
    C 求完数 水仙花数 完全平方数 素数 2+22+222... PI
    C 字串匹配
    C 约瑟夫环[数组]
    C 鞍点所在行是最大值所在列是最小值
    C 创建链表并将信息存储在二进制文件中再读取
    C 统计字符数 行数
  • 原文地址:https://www.cnblogs.com/pipu-qiao/p/5946891.html
Copyright © 2011-2022 走看看