日期格式化,时间戳转化

时间转换时间戳

date.valueOf();

时间戳转化为时分秒

function formatDateTime(date) { //之间转换格式
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    var second = date.getSeconds();
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
};
formatDateTime(new Date())

 

 时间戳转换天数

function dateFormat(times) {
    let s = Math.floor(times / 1000 % 60) < 10 ? ('0' + Math.floor(times / 1000 % 60)) : (Math.floor(times / 1000 % 60))
    let m = Math.floor(times / 1000 / 60 % 60) < 10 ? ('0' + Math.floor(times / 1000 / 60 % 60)) : (Math.floor(times / 1000 / 60 % 60))
    let h = Math.floor(times / 1000 / 60 / 60 % 24) < 10 ? ('0' + Math.floor(times / 1000 / 60 / 60 % 24)) : (Math.floor(times / 1000 / 60 / 60 % 24))
    let d = Math.floor(times / 1000 / 60 / 60 / 24) < 10 ? ('0' + Math.floor(times / 1000 / 60 / 60 / 24)) : (Math.floor(times / 1000 / 60 / 60 / 24))
    return result = d + '天' + h + '小时' + m + "分" + s + '秒'
}
dateFormat(new Date().valueOf())

 

posted @ 2021-12-21 16:57  从入门到入土  阅读(112)  评论(0编辑  收藏  举报