js new Date()參數格式

最近在寫頁面使用new Date()獲取時間戳在ie瀏覽器中測試發現無效;後來發現是參數格式問題,php

new Date()參數格式以下:html

一、用整數初始化日期對象 
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) 按給定的參數建立一日期對象正則表達式

二、用字符串初始化日期對象 
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) 對象,表示全局替換-爲/。瀏覽器

三、用毫秒時間戳初始化日期對象 
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複製過來的htm

 

如下是我的踩得坑:對象

一、在IE瀏覽器中不支持格式「2017-08-08」,須要轉換爲「2017/08/08」字符串

後續繼續踩坑。。。。。。get

相關文章
相關標籤/搜索