日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

javascript时间戳和日期字符串相互转换

發布時間:2023/12/19 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javascript时间戳和日期字符串相互转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 <script type="text/javascript"> 5 // 獲取當前時間戳(以s為單位) 6 var timestamp = Date.parse(new Date()); 7 timestamp = timestamp / 1000; 8 //當前時間戳為:1403149534 9 console.log("當前時間戳為:" + timestamp); 10 11 // 獲取某個時間格式的時間戳 12 var stringTime = "2014-07-10 10:21:12"; 13 var timestamp2 = Date.parse(new Date(stringTime)); 14 timestamp2 = timestamp2 / 1000; 15 //2014-07-10 10:21:12的時間戳為:1404958872 16 console.log(stringTime + "的時間戳為:" + timestamp2); 17 18 // 將當前時間換成時間格式字符串 19 var timestamp3 = 1403058804; 20 var newDate = new Date(); 21 newDate.setTime(timestamp3 * 1000); 22 // Wed Jun 18 2014 23 console.log(newDate.toDateString()); 24 // Wed, 18 Jun 2014 02:33:24 GMT 25 console.log(newDate.toGMTString()); 26 // 2014-06-18T02:33:24.000Z 27 console.log(newDate.toISOString()); 28 // 2014-06-18T02:33:24.000Z 29 console.log(newDate.toJSON()); 30 // 2014年6月18日 31 console.log(newDate.toLocaleDateString()); 32 // 2014年6月18日 上午10:33:24 33 console.log(newDate.toLocaleString()); 34 // 上午10:33:24 35 console.log(newDate.toLocaleTimeString()); 36 // Wed Jun 18 2014 10:33:24 GMT+0800 (中國標準時間) 37 console.log(newDate.toString()); 38 // 10:33:24 GMT+0800 (中國標準時間) 39 console.log(newDate.toTimeString()); 40 // Wed, 18 Jun 2014 02:33:24 GMT 41 console.log(newDate.toUTCString()); 42 43 Date.prototype.format = function(format) { 44 var date = { 45 "M+": this.getMonth() + 1, 46 "d+": this.getDate(), 47 "h+": this.getHours(), 48 "m+": this.getMinutes(), 49 "s+": this.getSeconds(), 50 "q+": Math.floor((this.getMonth() + 3) / 3), 51 "S+": this.getMilliseconds() 52 }; 53 if (/(y+)/i.test(format)) { 54 format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); 55 } 56 for (var k in date) { 57 if (new RegExp("(" + k + ")").test(format)) { 58 format = format.replace(RegExp.$1, RegExp.$1.length == 1 59 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length)); 60 } 61 } 62 return format; 63 } 64 console.log(newDate.format('yyyy-MM-dd h:m:s')); 65 66 </script>

?

總結

以上是生活随笔為你收集整理的javascript时间戳和日期字符串相互转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。