首页 > Web开发 > 详细

JS代码格式化时间戳

时间:2019-12-13 21:55:48      阅读:94      评论:0      收藏:0      [点我收藏+]

一、[24小时制]yyyy-MM-dd HH:mm:ss

new Date().toJSON() // 2019-12-13T13:12:32.265Z

通过上面的方法,基本就可以将日期格式化,然后稍加处理就能得到预期结果

// new Date().getTime() 等价于  +new Date()
function formartLongTime(time=+new Date()) {
    var date = new Date(time + 8 * 3600 * 1000); // 增加8小时得到东八区时间
    return date.toJSON().substr(0, 19).replace('T', ' ');
}

二、[12小时制]yyyy/MM/dd HH:mm:ss

new Date().toLocaleString() // 2019/12/13 下午9:24:43

同样,使用上述方法即可完成格式化

function formartLongTime(time=+new Date()) { 
    return new Date(time).toLocaleString(); 
}  // "2019/12/13 下午9:34:54"

JS代码格式化时间戳

原文:https://www.cnblogs.com/dafengdeai/p/12037408.html

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