首页 前端知识 时间戳转换成年月日时分秒

时间戳转换成年月日时分秒

2024-01-24 15:01:03 前端知识 前端哥 194 874 我要收藏

1.使用new Date()将时间戳转换成时间

var date = new Date(time);
复制

2.提取年

var Y = date.getFullYear() + '-';
复制

3.提取月判断是否需要补0操作

var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
复制

4.获取日判断是否需要补0操作

var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
复制

5.获取时判断是否需要补0操作

var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
复制

6.获取分判断是否需要补0操作

var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes());
复制

7.获取秒判断是否需要补0操作

var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
复制

全代码

time(time){
var date = new Date(time);
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes());
var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
var time1 = Y + M + D + h + m;
}
复制

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

js向上取整

2024-02-03 16:02:53

9、jQuery

2024-02-03 12:02:49

jQuery 遍历方法总结

2024-02-03 12:02:26

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