您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页jsnewDate()参数格式

jsnewDate()参数格式

来源:爱go旅游网
jsnewDate()参数格式

最近在写页⾯使⽤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秒)起⾄现在的总秒数。时间戳唯⼀地标识某⼀刻的时间。以上是在复制过来的

以下是个⼈踩得坑:

1、在IE浏览器中不⽀持格式“2017-08-08”,需要转换为“2017/08/08”后续继续踩坑。。。。。。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务