发布时间:1H内显示xx分钟前,1H上24H内显示xx小时前,超过24H显示具体日期。
程序员文章站
2022-03-07 21:45:32
// • 发布时间:1H内显示xx分钟前,1H上24H内显示xx小时前,超过24H显示具体日期。 differencefunction preTime(publishTime) {if(publishTime instanceof Date) {let _publishTime = publishTime.getTime();let _nowDate = new Date();let _nowTime = _nowDate.g....
// • 发布时间:1H内显示xx分钟前,1H上24H内显示xx小时前,超过24H显示具体日期。 difference
function preTime(publishTime) {
if(publishTime instanceof Date) {
let _publishTime = publishTime.getTime();
let _nowDate = new Date();
let _nowTime = _nowDate.getTime();
let differenceTime = _nowTime - _publishTime;
// 定义1小时时间戳
let oneHour = 1000 * 60 * 60;
// 定义24小时时间戳
let tfHours = 1000 * 60 * 60 * 24;
console.log(differenceTime);
// 1H内显示xx分钟前
if(differenceTime < oneHour) {
return `${(differenceTime/1000/60).toFixed(0)}分钟前`
}
// 1H上24H内显示xx小时前
if(differenceTime < tfHours) {
return `${(differenceTime/1000/60/60).toFixed(0)}小时前`
}
// 超过24H显示具体日期
let _year = publishTime.getFullYear();
let _month = publishTime.getMonth();
let _day = publishTime.getDate();
return `${_year}/${_month}/${_day}`
} else {
throw new Error('参数dateObj不是Date实例')
}
}
本文地址:https://blog.csdn.net/qq_40860137/article/details/107485101
上一篇: selenium基础
下一篇: webpack-搭建Vue