js 评论时间换算

Posted 看看时间

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 评论时间换算相关的知识,希望对你有一定的参考价值。

//时间戳换算
let dateTime=2020-10-10 10:10:10
getDateDiff(dateTime){
let dateTimeStamp = new Date(dateTime).getTime();
// console.log(dateTimeStamp)
let result = ‘‘;
let minute = 1000 * 60;
let hour = minute * 60;
let day = hour * 24;
let halfamonth = day * 15;
let month = day * 30;
let year = day * 365;
let now = new Date().getTime();
// console.log(now)
let diffValue = now - dateTimeStamp;
// console.log(diffValue)
if (diffValue < 0) {
return;
}
let monthEnd = diffValue / month;
let weekEnd = diffValue / (7 * day);
let dayEnd = diffValue / day;
let hourEnd = diffValue / hour;
let minEnd = diffValue / minute;
let yearEnd = diffValue / year;
if (yearEnd >= 1) {
result = dateTime;
// result = "" + parseInt(yearEnd) + "年前";

} else if (monthEnd >= 1) {
result = "" + parseInt(monthEnd) + "月前";
} else if (weekEnd >= 1) {
result = "" + parseInt(weekEnd) + "周前";
} else if (dayEnd >= 1) {
result = "" + parseInt(dayEnd) + "天前";
} else if (hourEnd >= 1) {
result = "" + parseInt(hourEnd) + "小时前";
} else if (minEnd >= 1) {
result = "" + parseInt(minEnd) + "分钟前";
} else {
result = "刚刚";
}

return result;
},


以上是关于js 评论时间换算的主要内容,如果未能解决你的问题,请参考以下文章

JS的时间差换算(String to 自己想要的时间格式)

js小数字换算科学计数法

进制换算,彻底明白js位运算符

js 给定时间,如'2013-08-30',换算和今天的天数差

一看就懂得移动端rem布局rem如何换算

华为OD机试真题 JS 实现货币单位换算2023 Q1 | 100分