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”

后續繼續踩坑。。。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM