js时间戳转日期,日期转时间戳3种方式 作者:admin 阅读:1974次 ### 时间戳转日期 - stamp = 时间戳,10或13位格式自动识别,10位则*1000 - nohour = 是否显示时分秒,默认显示 ```javascript 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种方式** ```javascript 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 ``` 赞一个 (15) 本站部分文章、数据、素材收集于互联网,一切版权均归源网站或原作者所有! 如果侵犯了您的权益请来信告知我们删除,邮箱:357234902@qq.com 标签:JavaScript 上一篇:用js把手机号中间4位(前7位)替换为*号,最常见隐藏手机号码方式 下一篇:js数组常用去重的几种方法(includes、sort、indexOf、splice、filter)