js new Date()参数格式

摘要:
时间戳唯一地标识某一刻的时间。

最近在写页面使用new Date()获取时间戳在ie浏览器中测试发现无效;后来发现是参数格式问题,

new Date()参数格式如下:

1、用整数初始化日期对象
var date1 = new Date(2017,06,06); console.log(date1); // Thu Jul 06 2017 00:00:00 GMT+0800 (中国标准时间)
var date1 = new Date(2017,1,1); console.log(date1); // Wed Feb 01 2017 00:00:00 GMT+0800 (中国标准时间)
var date1 = new Date(2017,01-2,01); console.log(date1); // Thu Dec 01 2016 00:00:00 GMT+0800 (中国标准时间)
var date1 =new Date(2017,06,06,06,06,06); console.log(date1); // Thu Jul 06 2017 06:06:06 GMT+0800 (中国标准时间)
说明:new Date( year, month, date, hrs, min, sec) 按给定的参数创建一日期对象

2、用字符串初始化日期对象
var date2 = new Date(“2017/06/06”); console.log(date2); // Tue Jun 06 2017 00:00:00 GMT+0800 (中国标准时间)
var date2 = new Date(“2017-08-08”); console.log(date2); // Tue Aug 08 2017 08:00:00 GMT+0800 (中国标准时间)
var date2 = new Date(“2017-9-9”); console.log(date2); // Sat Sep 09 2017 00:00:00 GMT+0800 (中国标准时间)
说明:如果字符串模式不支持短横杠模式,则进行字符串替换:
var strTime=”2011-04-16”;
var date2= new Date(Date.parse(strTime.replace(/-/g, “/”))); // /-/g为正则表达式(RegExp) 对象,表示全局替换-为/。

3、用毫秒时间戳初始化日期对象
var timestamp=new Date().getTime(); console.log( new Date(timestamp) ); //Tue Jun 06 2017 11:06:59 GMT+0800 (中国标准时间)
var date3 = new Date( timestamp - 1 * 60 * 60 * 1000); console.log(date3); // Tue Jun 06 2017 10:06:59 GMT+0800 (中国标准时间)
说明:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。时间戳唯一地标识某一刻的时间。

以上是在http://www.php.cn/js-tutorial-389248.html复制过来的

以下是个人踩得坑:

1、在IE浏览器中不支持格式“2017-08-08”,需要转换为“2017/08/08”

后续继续踩坑。。。。。。

免责声明:文章转载自《js new Date()参数格式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue使用render函数创建具名插槽unity editor模式下读取文件夹资源下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

js---通配符选择器

原味转自:http://blog.sina.com.cn/s/blog_6e001be701017kaz.html 1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code']");//id属性以code结束的所有input标签 $("input[id*=...

关于shell的单引号和双引号转义 以及特殊符号相关

关于shell的单引号和双引号转义 以及特殊符号相关 20140603 Chenxin shell转义,单引号与双引号,反撇号 1、转义 单引号和双引号都能关闭shell对特殊字符的处理。 不同的是,双引号没有单引号严格,单引号关闭所有有特殊作用的字符,而双引号只要求shell忽略大多数,具体的说,就是以下符号在双引号内仍起效果: ①美元符号 $ 在双引号...

protobuf编译

win10   cmake   vs2017编译 protobuf编译 cmake Selecting Windows SDK version 10.0.17134.0 to target Windows 10.0.19042. The C compiler identification is MSVC 19.15.26732.1 The CXX com...

js图片全屏

html, body {height:100%; } img {display:block;margin:100px auto 0;width:900px;cursor:pointer; } /*webkit和IE在元素进入全屏后,会保持元素原有的尺寸,所以需要通过css伪类进...

js中动态载入css js样式

js中动态载入css样式,方法如下: //<link rel="stylesheet" type="text/css" href="http://t.zoukankan.com/http://css.static.m1905.cn/base.min.css"> var addCssLink =function(url){ var lin...

js模拟发送 FormData数据

后台express需要connect-multiparty模块接收formData的数据类型 class ourFormData { constructor(data, rs) { return new String((function (data, rs) { let data_string = ' '...