js时间戳转日期,日期转时间戳3种方式
作者:Miao 阅读:3434次
时间戳转日期
stamp = 时间戳,10或13位格式自动识别,10位则*1000
nohour = 是否显示时分秒,默认显示
function formatTime(stamp, nohour = true) { const time = new Date(stamp.toString().length === 10 ? stamp * 1000 : stamp); const year = time.getFullYear(); const month = time.getMonth() + 1; const day = time.getDate(); const hour = time.getHours(); const minute = time.getMinutes(); const second = time.getSeconds(); if (!nohour) { return year + '-' + month + '-' + day } else { return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; } } console.log(formatTime(1536224616), formatTime(1536224616, false)); // 输出结果:2018-9-6 17:3:36,2018-9-6
日期转时间戳
日期转时间戳有3种方式
let date = new Date("2018-09-06 17:03:36"); console.log(date.getTime()); // 1536224616000 console.log(date.valueOf()); // 1536224616000 console.log(Date.parse(date)); // 1536224616000
本站部分文章、数据、素材收集于网络,所有版权均归源网站或原作者所有!
如果侵犯了您的权益,请来信告知我们下线删除,邮箱:357234902@qq.com