js将时间戳翻译成日期格式

 

js将时间戳翻译成日期格式

function timestamp2time(stamp){
    var date = new Date(stamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
    console.log("=== date ===");
    console.log(date);
    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();
    MI = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    S = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + " " + H + ":" + MI + ":" + S;
}

传入的“stamp”是一个由“new Date()”函数获取的一个对象,它包含一个时间戳

这个函数会按照当前系统的时区,把这个时间戳转换为“年-月-日 时:分:秒”的格式

posted @ 2021-07-21 19:14  94那抹微笑  阅读(162)  评论(0编辑  收藏  举报