首页 > Web开发 > 详细

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

时间:2021-07-21 23:06:14      阅读:25      评论:0      收藏:0      [点我收藏+]

 

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()”函数获取的一个对象,它包含一个时间戳

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

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

原文:https://www.cnblogs.com/qscfyuk/p/15041052.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!