/**
* 时间十三位转换
*/
function dateFormat(value) {
var date = new Date(value);
const Y = date.getFullYear() + '-';
const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
return Y + M + D + h + m + s;
}
/**
* 时间十三位转换为多久前
*/
function time(value) {
const time = Date.now() - new Date(value)
/*
1秒 1000
1分钟 1000 * 60 60000
1小时 1000 * 60 * 60 3600000
1天 1000 * 60 * 60 * 24 86400000
1个月 1000 * 60 * 60 * 24 * 30 2592000000
1年 1000 * 60 * 60 * 24 * 365 31536000000
*/
const times = [31536000000, 2592000000, 86400000, 3600000, 60000, 1000]
const units = ['年前', '月前', '天前', '小时前', '分钟前', '秒前']
for (let i = 0; i < times.length; i++) {
const res = time / times[i]
if (res >= 1) {
return Math.round(res) + units[i]
}
}
}
前端时间格式化方法
最新推荐文章于 2023-05-25 15:29:57 发布