JavaScrip(JS)t時間戳與時間日期間相互轉換


在javascript(js)中如何對時間戳與時間日期之間的相互轉換以及用法。(轉)

javascript的getTime()方法可以輸入時間戳

利用js輸入現在時間以及指定時間的時間戳

代碼

1 //將當前的時間轉換成時間戳
2 var now = new Date();
3 console.log(now.getTime());
4 //將指定的日期轉換成時間戳
5 var t = "2018-07-23 20:5:30";  
6 var T = new Date(t); 
7 console.log(T.getTime());
8  //console.log(),為控制台打印

代碼示圖

 

JavaScrip(JS)t時間戳與時間日期間相互轉換

 

代碼運行結果

查看瀏覽器控制台

JavaScrip(JS)t時間戳與時間日期間相互轉換

 

利用js的時間戳,輸出時間日期格式

代碼

1 //輸出現在時間的日期格式
2 console.log(new Date());
3 //輸入一個時間戳來轉換成時間日期格式
4 //飛鳥慕魚博客
5  var t = 1528459530000;  
6  console.log(new Date(t));

代碼顯圖

 

JavaScrip(JS)t時間戳與時間日期間相互轉換

 代碼運行結果

 

JavaScrip(JS)t時間戳與時間日期間相互轉換

 

關於javascript時間戳與時間日期格式轉換的代碼補充

代碼

 1 <script type="text/javascript">
 2     // 獲取當前時間戳(以s為單位)
 3      var timestamp = Date.parse(new Date());
 4     timestamp = timestamp / 1000;
 5     //當前時間戳為:1403149534
 6      console.log("當前時間戳為:" + timestamp);
 7     // 獲取某個時間格式的時間戳
 8     var stringTime = "2018-07-10 10:21:12";
 9     var timestamp2 = Date.parse(new Date(stringTime));
10     timestamp2 = timestamp2 / 1000;
11     //2018-07-10 10:21:12的時間戳為:1531189272
12     console.log(stringTime + "的時間戳為:" + timestamp2);
13     // 將當前時間換成時間格式字符串
14      var timestamp3 = 1531189272;
15      var newDate = new Date();
16      newDate.setTime(timestamp3 * 1000);
17     // Wed Jun 18 2018
18     console.log(newDate.toDateString());
19     // Wed, 18 Jun 2018 02:33:24 GMT
20      console.log(newDate.toGMTString());
21     // 2018-06-18T02:33:24.000Z
22     console.log(newDate.toISOString());
23     // 2018-06-18T02:33:24.000Z
24     console.log(newDate.toJSON());
25     // 2018年6月18日
26     console.log(newDate.toLocaleDateString());
27     // 2018年6月18日 上午10:33:24
28     console.log(newDate.toLocaleString());
29     // 上午10:33:24
30      console.log(newDate.toLocaleTimeString());
31     // Wed Jun 18 2018 10:33:24 GMT+0800 (中國標准時間)
32     console.log(newDate.toString());
33     // 10:33:24 GMT+0800 (中國標准時間)
34     console.log(newDate.toTimeString());
35      // Wed, 18 Jun 2018 02:33:24 GMT
36      console.log(newDate.toUTCString());
37 Date.prototype.format = function(format) {
38     var date = {
39         "M+": this.getMonth() + 1,
40         "d+": this.getDate(),
41         "h+": this.getHours(),
42         "m+": this.getMinutes(),
43         "s+": this.getSeconds(),
44         "q+": Math.floor((this.getMonth() + 3) / 3),
45         "S+": this.getMilliseconds()
46     };
47     if (/(y+)/i.test(format)) {
48             format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
49         }
50     for (var k in date) {
51        if (new RegExp("(" + k + ")").test(format)) {
52             format = format.replace(RegExp.$1, RegExp.$1.length == 1
53                 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
54        }
55      }
56      return format;
57  }
58  console.log(newDate.format('yyyy-MM-dd h:m:s'));
59  </script>

代碼運行結果:

 

JavaScrip(JS)t時間戳與時間日期間相互轉換

 


免責聲明!

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



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