首页 前端知识 js时间戳转年月日时分秒代码

js时间戳转年月日时分秒代码

2024-01-17 18:01:08 前端知识 前端哥 565 668 我要收藏
以下是将时间戳转换为年月日时分秒的 JavaScript 代码:
复制
function formatDate(timestamp) {
const date = new Date(timestamp * 1000);
const year = date.getFullYear();
const month = ('0' (date.getMonth() 1)).slice(-2);
const day = ('0' date.getDate()).slice(-2);
const hours = ('0' date.getHours()).slice(-2);
const minutes = ('0' date.getMinutes()).slice(-2);
const seconds = ('0' date.getSeconds()).slice(-2);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
复制

你可以调用函数 formatDate 并传入一个时间戳作为参数,它将返回类似于 "2022-01-01 12:34:56" 的格式化日期字符串。

转载请注明出处或者链接地址:https://www.qianduange.cn//article/98.html
标签
jsAI生成
评论
还可以输入200
共0条数据,当前/页
发布的文章

jQuery AJAX请求的统一封装

2024-02-01 12:02:53

jQuery知识学习

2024-02-01 12:02:53

JQuery——动画效果

2024-02-01 12:02:52

jQuery复习

2024-02-01 12:02:51

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!