时间戳转日期
发布日期:2021-05-15 23:37:34 浏览次数:18 分类:精选文章

本文共 1419 字,大约阅读时间需要 4 分钟。

// timestampToTime 函数用于将时间戳转换为日期时间字符串格式
function timestampToTime(timestamp) {
const date = new Date(timestamp); // 创建日期对象
const year = date.getFullYear(); // 获取年份
const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 获取月份,转为两位字符串
const day = date.getDate().toString().padStart(2, '0'); // 获取日期,转为两位字符串
const hours = date.getHours().toString().padStart(2, '0'); // 获取小时,转为两位字符串
const minutes = date.getMinutes().toString().padStart(2, '0'); // 获取分钟,转为两位字符串
const seconds = date.getSeconds().toString().padStart(2, '0'); // 获取秒数,转为两位字符串
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
// 调用示例
console.log(timestampToTime(1625098400000)); // 2021-07-31 12:40:00
// 或根据具体需求使用
${javascript:timestampToTime(${timestamp})}

 

 
function timestampToTime(timestamp) {
const date = new Date(timestamp);
const Y = date.getFullYear() + "-";
const M = (date.getMonth() + 1).toString().padStart(2, '0') + "月";
const D = date.getDate().toString().padStart(2, '0') + "日 ";
const h = date.getHours().toString().padStart(2, '0') + ":";
const m = date.getMinutes().toString().padStart(2, '0');
const s = date.getSeconds().toString().padStart(2, '0');
return Y + M + D + h + m + s;
}
上一篇:微信小程序中的分类联动
下一篇:封装两个时间戳相差多长时间,返回时分,天,或者已经结束

发表评论

最新留言

不错!
[***.144.177.141]2025年04月28日 15时18分58秒