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”

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

posted @ 2019-03-06 16:05  H-LI  阅读(16239)  评论(0编辑  收藏  举报