轨道 · 写下来的字
jsformat
js 时间戳格式化
// @param value 毫秒
// @param fmt "yyyy-MM-dd hh:mm:ss" | yyyy/MM/dd hh:mm:ss
function formatTimestamp(value, fmt) {
let getDate = new Date(value)
let o = {
'M+': getDate.getMonth() + 1,
'd+': getDate.getDate(),
'h+': getDate.getHours(),
'm+': getDate.getMinutes(),
's+': getDate.getSeconds(),
'q+': Math.floor((getDate.getMonth() + 3) / 3),
'S': getDate.getMilliseconds()
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (let k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
}
}
return fmt
}
const r = formatTimestamp(1605780000 * 1000, "yyyy-MM-dd hh:mm:ss")
console.log(r)← 回到文章