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.
safari 日期对象新建new Date( timeStr ) 参数TimeStr格式的更多相关文章
- 在EL表达式或者Struts标签库中格式化日期对象,即将Date转换为yyyy-MM-dd格式
一.EL表达式 首先,在jsp页面引入<fmt> tags,<%@ taglib prefix="fmt" uri="http://java.sun.c ...
- 常用封装--Date篇--获取格式化的日期对象
虽然日期对象可以使用new Date()来获取,但是对于其格式却必须进行相应的转换,才能成为开发者想要的格式. 这里提供了一个封装的方法,通过结合正则表达式的使用,达到了可以对时间对象进行处理,生成多 ...
- js 日期对象Date以及传参
创建一个日期对象,日期对象可传参new Date() 创建日期对象getFullYear() 获取年份 getMonth() 获取月份 返回值是 0(一月) 到 11(十二月) 之间的一个整数 get ...
- 日期对象-Date
新建日期对象 var date = new Date(); getTime() 从 1970年 1月 1日开始计算到 Date 对象中的时间之间的毫秒数. getFullYear() ...
- JavaScript中Date(日期对象),Math对象--学习笔记
Date对象 1.什么是Date对象? 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 语法:var Udate=new Date(); 注:初始值为当前时间(当前电脑系统 ...
- JavaScript Date日期对象以及日期格式化方法
前言 Date对象是javascript语言中内置的数据类型,用于提供日期和时间的操作接口.Date对象是在早期java中的java.util.Date类基础上创建的,为此,Date类型使用自UTC1 ...
- (70)Wangdao.com第十一天_JavaScript 日期对象 Date
日期对象 Date 表示一个时间 Date 对象是 JavaScript 原生的时间库 它以1970年1月1日00:00:00作为时间的零点,可以表示的时间范围是前后各1亿天(单位为毫秒) 时间零点( ...
- js之Date(日期对象)
通过日期对象我们可以进行一些对日期时间的操作处理 一.日期对象的创建: var myDate=new Date() 二.Date对象方法: Link:http://www.w3school.com.c ...
- js动态获取当前系统时间+js字符串转换为date日期对象
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- CTE递归查询
WITH ctetest(AgencyID,ParentAgencyID,level)AS ( SELECT AgencyID,ParentAgencyID,0 level FROM dbo.Web_ ...
- node模块系统常用命令
node模块系统常用命令 命令 示例 备注 安装模块 npm install commander 最新版本 npm install commander@1.0.0 指定版本 npm install c ...
- 运行impala tpch
1.安装git和下载tpc-h-impala脚步 [root@ip-172-31-34-31 ~]# yum install git [root@ip-172-31-34-31 ~]# git clo ...
- LINUX:read、array、declare
read:要读取来自键盘输入的变量 使用规则: read [-pt] variale 选项与参数: -p:后面接提示字符: -t:后面接等待的“秒数”: 如果read之后不加任何参数,直接加上变量名称 ...
- C++ TR1 Function Bind
在C++ 11出现以前,C++的事件一般是通过回调形试来实现,如 void (*func)(int,int,int),其实际上是一种函数指针,在C中调用时是直接写函数名在参数列表中,而在C++中,大部 ...
- Asp.net MVC 示例
public ActionResult Browse(string id){ using (musicstoreEntities db = new musicstoreEntities( ...
- TCP协议与UDP协议的区别
TCP协议与UDP协议的区别(转) 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UDP协议的区别,我觉得这是没有从本质上弄清楚网络通信! ...
- 设置table距离顶部位置
//UIEdgeInsetsMake———— UIEdgeInsets insets = {top, left, bottom, right} self.tableView.contentInset ...
- js②
操作符 ECMA-262描述了一组用于操作数据值的操作符,包括算术操作符(如加号和减号).位操作符.关系操作符和相等操作符. 一元操作符 递增和递减操作符(++ --) 一元加和减操作符 对非数值应用 ...
- 常用js字符串方法学习总结
2016-06-15 js数组和字符串方法有很多,并且有一部分在使用的过程中有很多方法是很容易被混淆的,今天来总结一下js中数组和字符串的方法. ♦数组(Array)的方法 1.push() 和 po ...