這篇文章將為大家詳細(xì)講解有關(guān)小程序如何獲取系統(tǒng)時間、時間戳?xí)r間以及時間戳加減,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

//獲取當(dāng)前時間戳
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
console.log("當(dāng)前時間戳為:" + timestamp);
//獲取當(dāng)前時間
var n = timestamp * 1000;
var date = new Date(n);
//年
var Y = date.getFullYear();
//月
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
//日
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
//時
var h = date.getHours();
//分
var m = date.getMinutes();
//秒
var s = date.getSeconds();
console.log("當(dāng)前時間:" +Y+M+D+h+":"+m+":"+s);
//轉(zhuǎn)換為時間格式字符串
console.log(date.toDateString());
console.log(date.toGMTString());
console.log(date.toISOString());
console.log(date.toJSON());
console.log(date.toLocaleDateString());
console.log(date.toLocaleString());
console.log(date.toLocaleTimeString());
console.log(date.toString());
console.log(date.toTimeString());
console.log(date.toUTCString());//時間、時間戳加減 以加一天舉例,聰明的你肯定觸類旁通 //加一天的時間戳: var tomorrow_timetamp = timestamp + 24 * 60 * 60; //加一天的時間: var n_to = tomorrow_timetamp * 1000; var tomorrow_date = new Date(n_to); //加一天后的年份 var Y_tomorrow = tomorrow_date.getFullYear(); //加一天后的月份 var M_tomorrow = (tomorrow_date.getMonth() + 1 < 10 ? '0' + (tomorrow_date.getMonth() + 1) : tomorrow_date.getMonth() + 1); //加一天后的日期 var D_tomorrow = tomorrow_date.getDate() < 10 ? '0' + tomorrow_date.getDate() : tomorrow_date.getDate(); //加一天后的時刻 var h_tomorrow = tomorrow_date.getHours(); //加一天后的分鐘 var m_tomorrow = tomorrow_date.getMinutes(); //加一天后的秒數(shù) var s_tomorrow = tomorrow_date.getSeconds();
關(guān)于“小程序如何獲取系統(tǒng)時間、時間戳?xí)r間以及時間戳加減”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
本文標(biāo)題:小程序如何獲取系統(tǒng)時間、時間戳?xí)r間以及時間戳加減-創(chuàng)新互聯(lián)
鏈接URL:http://chinadenli.net/article0/cddcoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站排名、微信小程序、服務(wù)器托管、外貿(mào)網(wǎng)站建設(shè)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容