Javascript時間戳和日期時間的相互轉換


跟后台對接的時候經常碰到時間格式的問題,有時返回的是時間戳,有時返回的是具體時間,需求又需要它們之間的轉換,所以干脆把之前遇到過的情況都給記錄下來,以供自己參考!

本文備注:(時間戳單位為毫秒ms,換算秒s需timestrap/1000;
例子使用時間為:'2017/5/11 11:42:18')

1.獲取當前日期的時間戳

1  var timestrap=Date.parse(new Date());
2  //或者
3  var timestrap=(new Date()).getTime();
4  console.log(timestrap);//1494474138000    

  我經常用的是(new Date()).getTime()這種方式,很少用Date.parse(new Date()),因為會轉換后三位毫秒數,不精確。

2.獲取具體時間格式的時間戳

1  var timestrap=(new Date('2017/5/11 11:42:18')).getTime();
2  //1494474138000

3.轉換指定時間戳

1  var time=new Date(1494474138000);
2  //Thu May 11 2017 11:42:18 GMT+0800 (中國標准時間)
3  //Thu May 11 2017 11:42:18 GMT+0800 (CST)

4. Date()

  定義:

1  var timestrap=1494474138000;
2  var newDate=new Date();
3  newDate.setTime(timestrap);
  Date()實例具體使用情況:
1  console.log(newDate.toLocaleString());//2017/5/11 上午11:42:18
2  console.log(newDate.toString());    //Thu May 11 2017 11:42:18 GMT+0800 (中國標准時間)
3  console.log(newDate.toDateString());//Thu May 11 2017 
4  console.log(newDate.toTimeString());//11:42:18 GMT+0800 (中國標准時間)
5  console.log(newDate.toGMTString()); //Thu, 11 May 2017 03:42:18 GMT
6  console.log(newDate.toUTCString()); //Thu, 11 May 2017 03:42:18 GMT
7  console.log(newDate.toISOString()); //2017-05-11T03:42:18.000Z
8  console.log(newDate.toJSON());      //2017-05-11T03:42:18.000Z

  //返回一個指定日期對象的年份;
1  console.log(newDate.getFullYear()); //2017,年

  //返回一個指定的日期對象的月份,返回一個0 到 11的整數值, 0 代表一月,1 代表二月,...
2  console.log(newDate.getMonth());   //4,月

  //返回一個指定的日期對象為一個月中的第幾天;
3  console.log(newDate.getDate());     //11,日

  //返回一個指定的日期對象的小時,返回一個0 到 23之間的整數值;
4  console.log(newDate.getHours());   //11,時

 //返回一個指定的日期對象的分鍾數,返回一個0 到 59的整數值;
5 console.log(newDate.getMinutes()); //42,分

  //返回一個指定的日期對象的秒數,返回一個 0 到 59 的整數值;
6  console.log(newDate.getSeconds());  //18,秒

 //返回一個指定的日期對象的毫秒數,返回一個0 到 999的整數;
7 console.log(newDate.getMilliseconds());//0,毫秒

  //返回一個具體日期中一周的第幾天,0 表示星期天,1 表示星期一,...
8  console.log(newDate.getDay());      //4,星期

  //返回值一個數值,表示從1970年1月1日0時0分0秒(UTC,即協調世界時)距離該日期對象所代表時間的毫秒數;
9  console.log(newDate.getTime());     //1494474138000

方法使用情況:
//解析一個表示日期的字符串,返回毫秒數 1 Date.parse('2017/5/11 11:42:18');//1494474138000

  //返回自 1970-1-1 00:00:00  UTC (時間標准時間)至今所經過的毫秒數,類型為Number;
2  Date.now(); //時間戳

 5.利用原型對象prototype來做時間格式的轉換

"M+": '月份',
"d+": '日',
"h+": '小時',
"m+": '分鍾',
"s+": '秒',
"q+": '季度',
"S+": '毫秒',
"y+": '年份',
 1   Date.prototype.format=function(format){
 2             var date={
 3                 "M+": this.getMonth()+1, 
 4                 "d+": this.getDate(), 
 5                 "h+": this.getHours(),
 6                 "m+": this.getMinutes(),
 7                 "s+": this.getSeconds(),
 8                 "q+": Math.floor((this.getMonth()+3)/3), 
 9                 "S+": this.getMilliseconds(), 
10             };
11             if(/(y+)/i.test(format)){
12                 format=format.replace(RegExp.$1,(this.getFullYear()+'').substr(4-RegExp.$1.length));
13             }
14             for(var i in date){
15                 if(new RegExp("("+i+")").test(format)){
16                     format=format.replace(RegExp.$1, RegExp.$1.length==1?date[i]:("00"+date[i]).substr((""+date[i]).length));
17                 }
18             }
19             return format;
20         }

  使用用例:

1 (new Date('2017-03-23 17:33:45')).format('yyyy/MM/dd h:m:s');
2 (new Date()).format('MM,qq');

 

~~~~~~~~~~~~~~~~~~~~~~~~~ 分割線der ~~~~~~~~~~~~~~~~~~~~~~~~~

2018-11-14補充:

近期和php后台對接時,發現輸出的時間戳為10位,JS需要換成13位的時間戳才能成功換算日期;

JAVA也是10位~~

完畢喲~

 


免責聲明!

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



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