日期格式化,時間戳轉化
阿新 • • 發佈:2021-12-21
時間轉換時間戳
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())