Live2D

Js时间戳转时间格式

废话不多说,直接上代码

Js时间戳转(yyyy:MM:dd hh:mm:ss)格式

// 标准时间转换成年月日时分秒(补0)
function getTime(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    let Y = date.getFullYear(),
        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),
        D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()),
        h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()),
        m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()),
        s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
    return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
}

console.log(
getTime('时间戳'));

 


posted @ 2022-11-16 15:53  键盘命  阅读(3940)  评论(0编辑  收藏  举报