js计算日期时间距离现在多久
//距离现在时间多久
function dateDiff(hisTime, nowTime) {if (!arguments.length) return '';var arg = arguments,now = arg[1] ? arg[1] : new Date().getTime(),diffValue = now - new Date(arg[0].replace(/-/g, '/')).getTime(),result = "",minute = 1000 * 60,hour = minute * 60,day = hour * 24,halfamonth = day * 15,month = day * 30,year = month * 12,_year = diffValue / year,_month = diffValue / month,_week = diffValue / (7 * day),_day = diffValue / day,_hour = diffValue / hour,_min = diffValue / minute;console.log(arg[0]);console.log(now, new Date(arg[0].replace(/-/g, '/')).getTime());console.log(diffValue);// if (new Date().toDateString() == convertDateFromString(hisTime).toDateString()) {// result.isToday = true;// }if (_year >= 1) result = parseInt(_year) + "年前";else if (_month >= 1) result = parseInt(_month) + "个月前";else if (_week >= 1) result = parseInt(_week) + "周前";else if (_day >= 1) result = parseInt(_day) + "天前";else if (_hour >= 1) result = parseInt(_hour) + "个小时前";else if (_min >= 1) result = parseInt(_min) + "分钟前";else result = "刚刚";return result;
}
调用
dateDiff(“2021-10-18 10:57”,null)
本文发布于:2024-02-02 08:00:53,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170683205442446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |