[index.js] 增加 formatDate 方法;[unisocket.js] event 改名 skevent

This commit is contained in:
Luk Lu
2020-06-01 13:13:25 +08:00
parent 76a58883be
commit 5ac4164173
2 changed files with 43 additions and 6 deletions

View File

@@ -93,4 +93,33 @@ module.exports = {
}
},
formatDate(date, format){
if (!(date instanceof Date)){
date = new Date(date)
}
if (!date.toJSON()) {
date = new Date()
}
format = (format && typeof format==='string')
? format
: 'yyyy-mm-dd HH:MM:SS'
let o = {
'm+': date.getMonth() + 1, //月份
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
'd+': date.getDate(), //日
'H+': date.getHours(), //小时
'M+': date.getMinutes(), //分
'S+': date.getSeconds(), //秒
's': date.getMilliseconds() //毫秒
}
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length))
for (var k in o){
if (new RegExp(`(${k})`).test(format))
format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)))
}
return format
},
}