javascript-時間戳記

來源:互聯網
上載者:User

標籤:時間格式   date()   asc   擷取   seconds   on()   bsp   console   log   

 

 1 // 擷取目前時間戳(以s為單位) 2 var timestamp = Date.parse(new Date()); 3 timestamp = timestamp / 1000; 4 console.log("目前時間戳為:" + timestamp); 5  6  7  8 // 擷取某個時間格式的時間戳記 9 var stringTime = "2017-12-06 21:51:12";10 var timestamp2 = Date.parse(new Date(stringTime));11 timestamp2 = timestamp2 / 1000;12 //2017-12-06 21:51:12的時間戳記為:151256827213 console.log(stringTime + "的時間戳記為:" + timestamp2);14 15 var timestamp3 = 1512567397;16 var newDate = new Date();17 newDate.setTime(timestamp3 * 1000);18 // Wed Dec 06 201719 console.log(newDate.toDateString());20 // Wed, 06 Dec 2017 13:36:37 GMT21 console.log(newDate.toGMTString());22 // 2017-12-06T13:36:37.000Z23 console.log(newDate.toISOString());24 // 2017-12-06T13:36:37.000Z25 console.log(newDate.toJSON().replace(/:\d{1,2}$/,‘‘));26 // 2017-12-6 27 console.log(newDate.toLocaleDateString().replace(/\/+/g,‘-‘));28 // 2017/12/6 下午9:36:3729 console.log(newDate.toLocaleString());30 // 下午9:36:3731 console.log(newDate.toLocaleTimeString());32 // Wed Dec 06 2017 21:36:37 GMT+0800 (中國標準時間)33 console.log(newDate.toString());34 // 21:36:37 GMT+0800 (中國標準時間)35 console.log(newDate.toTimeString());36 // Wed, 06 Dec 2017 13:36:37 GMT37 console.log(newDate.toUTCString());

  

 1 var newDate = new Date(); 2 Date.prototype.format = function(format) { 3        var date = { 4           "M+": this.getMonth() + 1, 5           "d+": this.getDate(), 6           "h+": this.getHours(), 7           "m+": this.getMinutes(), 8           "s+": this.getSeconds(), 9           "q+": Math.floor((this.getMonth() + 3) / 3),10           "S+": this.getMilliseconds()11        };12        if (/(y+)/i.test(format)) {13               format = format.replace(RegExp.$1, (this.getFullYear() + ‘‘).substr(4 - RegExp.$1.length));14        }15        for (var k in date) {16               if (new RegExp("(" + k + ")").test(format)) {17                      format = format.replace(RegExp.$1, RegExp.$1.length == 1? date[k] : ("00" + date[k]).substr(("" + date[k]).length));18               }19        }20        return format;21 }22 console.log(newDate.format(‘yyyy-MM-dd h:m:s‘));

 

javascript-時間戳記

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.