码迷,mamicode.com
首页 > Web开发 > 详细

js 评论时间换算

时间:2020-10-22 22:38:37      阅读:32      评论:0      收藏:0      [点我收藏+]

标签:day   ediff   date   date()   div   log   lse   时间   The   

//时间戳换算
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 评论时间换算

标签:day   ediff   date   date()   div   log   lse   时间   The   

原文地址:https://www.cnblogs.com/wangjianping123/p/13856621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!