Vue就这样写:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实现发布了多久的时间描述</title>
</head>
<body>
<h1 id="app">
<!-- 函数的参数自行填写 或者从后端获取来执行 -->
该用户上一次发布时间为:<a v-text='handlePublishTimeDesc("2020-6-12 15:33:0.0")'></a>
</h1>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
new Vue({
el:"#app",
methods:{
getRelease:function(time) {
//获取发布时间
var arr = time.split(/[- :]/),
//将其转换成格林威治标准时间 (GMT)
_date = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]),
//再转成毫秒数
timeStr = Date.parse(_date)
console.log(arr);
console.log(_date);
console.log(timeStr);
//return出去
return timeStr
},
handlePublishTimeDesc:function(post_modified){
// 拿到当前时间戳和发布时的时间戳,然后得出时间戳差
//获取当前时间
var nowTime = new Date();
//部分浏览器不兼容此转换,建议所以对此进行补充
//(指定调用自己定义的函数进行生成发布时间的时间戳)
// var postTime = new Date(post_modified);
//var timeDiff = nowTime.getTime() - postTime.getTime();
//上面一行代码可以换成以下(兼容性的解决)
var timeDiff = nowTime.getTime() - this.getRelease(post_modified);
// 单位换算
var min = 60 * 1000;
var hour = min * 60;
var day = hour * 24;
var week = day * 7;
var month = week * 4;
var year = month * 12;
// 计算发布时间距离当前时间的周、天、时、分
var exceedyear = Math.floor(timeDiff / year);
var exceedmonth = Math.floor(timeDiff / month);
var exceedWeek = Math.floor(timeDiff / week);
var exceedDay = Math.floor(timeDiff / day);
var exceedHour = Math.floor(timeDiff / hour);
var exceedMin = Math.floor(timeDiff / min);
// 最后判断时间差到底是属于哪个区间,然后return
if (exceedyear < 100 && exceedyear > 0) {
return exceedyear + '年前';
} else {
if (exceedmonth < 12 && exceedmonth > 0) {
return exceedmonth + '月前';
} else {
if (exceedWeek < 4 && exceedWeek > 0) {
return exceedWeek + '星期前';
} else {
if (exceedDay < 7 && exceedDay > 0) {
return exceedDay + '天前';
} else {
if (exceedHour < 24 && exceedHour > 0) {
return exceedHour + '小时前';
} else {
return exceedMin + '分钟前';
}
}
}
}
}
}
}
})
</script>
js中就这样写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getRelease(time) {
//获取发布时间
var arr = time.split(/[- :]/),
//将其转换成格林威治标准时间 (GMT)
_date = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]),
//再转成毫秒数
timeStr = Date.parse(_date)
console.log(arr);
console.log(_date);
console.log(timeStr);
//return出去
return timeStr
}
function handlePublishTimeDesc(post_modified) {
// 拿到当前时间戳和发布时的时间戳,然后得出时间戳差
//获取当前时间
var nowTime = new Date();
//部分浏览器不兼容此转换建议所以对此进行补充(指定调用自己定义的函数进行生成发布时间的时间戳)
// var postTime = new Date(post_modified);
//var timeDiff = nowTime.getTime() - postTime.getTime();
//上面一行代码可以换成以下(兼容性的解决)
var timeDiff = nowTime.getTime() - getRelease(post_modified);
// 单位换算
var min = 60 * 1000;
var hour = min * 60;
var day = hour * 24;
var week = day * 7;
var month = week * 4;
var year = month * 12;
// 计算发布时间距离当前时间的周、天、时、分
var exceedyear = Math.floor(timeDiff / year);
var exceedmonth = Math.floor(timeDiff / month);
var exceedWeek = Math.floor(timeDiff / week);
var exceedDay = Math.floor(timeDiff / day);
var exceedHour = Math.floor(timeDiff / hour);
var exceedMin = Math.floor(timeDiff / min);
// 最后判断时间差到底是属于哪个区间,然后return
if (exceedyear < 100 && exceedyear > 0) {
return exceedyear + '年前';
} else {
if (exceedmonth < 12 && exceedmonth > 0) {
return exceedmonth + '月前';
} else {
if (exceedWeek < 4 && exceedWeek > 0) {
return exceedWeek + '星期前';
} else {
if (exceedDay < 7 && exceedDay > 0) {
return exceedDay + '天前';
} else {
if (exceedHour < 24 && exceedHour > 0) {
return exceedHour + '小时前';
} else {
return exceedMin + '分钟前';
}
}
}
}
}
}
alert(handlePublishTimeDesc("2020-6-12 15:33:0.0"))
</script>
</body>
</html>