小程序加盟平臺(tái)快速排名優(yōu)化
javascript【格式化時(shí)間日期】 操作:
(1) 日期格式化代碼
/*** 日期格式化函數(shù)<br/>* 調(diào)用格式:需要使用日期對(duì)象調(diào)用* <p> new Date().Format("yyyy/MM/dd HH:mm:ss"); </p>* @param fmt 日期格式* @returns {*} 返回格式化后的日期* @constructor*/
Date.prototype.Format = function (fmt) {var object = {"M+": this.getMonth() + 1, // 月"d+": this.getDate(), // 日"H+": this.getHours(), // 時(shí)"m+": this.getMinutes(), // 分"s+": this.getSeconds(), // 秒"q+": Math.floor((this.getMonth() + 3) / 3), // 季度"S": this.getMilliseconds() // 毫秒};// 正則表達(dá)式if (/(y+)/.test(fmt)) {fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));}for (var pattern in object) {if (new RegExp("(" + pattern + ")").test(fmt)) {fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (object[pattern]) : (("00" + object[pattern]).substr(("" + object[pattern]).length)));}}return fmt;
}
(2) 函數(shù)調(diào)用
使用一個(gè)Date對(duì)象去調(diào)用Format函數(shù)即可。
時(shí)間戳轉(zhuǎn)換,如果是時(shí)間戳需要轉(zhuǎn)換,則需要注意的是,時(shí)間戳是10位長度的,還是13位長度的,*如果是10位長度的時(shí)間戳,那么就需要乘以1000后,在調(diào)用Format進(jìn)行轉(zhuǎn)換*。
舉例如下:
// 調(diào)用函數(shù)
// 年月日
var date1 = new Date().Format("yyyy/MM/dd")
console.log(date1)
// 年月日 時(shí)分秒 毫秒
var date2 = new Date().Format("yyyy/MM/dd HH:mm:ss.S")
console.log(date2)
// 年月日 時(shí)分秒 毫秒 季度
var date3 = new Date().Format("yyyy/MM/dd HH:mm:ss.S qq")
console.log(date3)
// 時(shí)間戳
var date4 = new Date(1609430400000).Format("yyyy/MM/dd HH:mm:ss.S qq")
console.log(date4)
代碼運(yùn)行結(jié)果如下: