自学内容网 自学内容网

通过js将时间戳转换为年:月:日 时:分:秒

let timestamp = Date.now();  
let time_format = timestampToTime(timestamp);  
console.log('timestamp == ' +timestamp);  
console.log('time_format == ' +time_format);  

//timestamp --> date time
function timestampToTime(timestamp){
    let date = new Date(timestamp);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0'+(date.getMonth()+1) : (date.getMonth()+1)) + '-'; 
    let D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' '; 
    let h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
    return Y+M+D+h+m+s;
}

运行结果如下:


原文地址:https://blog.csdn.net/qq_48968230/article/details/145190384

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!