JS代码片段:一个日期离现在多久了
Posted Yanyd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS代码片段:一个日期离现在多久了相关的知识,希望对你有一定的参考价值。
var dateDiff = function(hisTime,nowTime){
if(!arguments.length) return ‘‘;
var arg = arguments,
now =arg[1]?arg[1]:new Date().getTime(),
diffValue = now - arg[0].getTime(),
result={
isToday:false
},
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;
if(new Date().toDateString()==hisTime.toDateString()){
result.isToday=true;
}
if(_year>=1) result.text=parseInt(_year) + "年前";
else if(_month>=1) result.text=parseInt(_month) + "个月前";
else if(_week>=1) result.text=parseInt(_week) + "周前";
else if(_day>=1) result.text=parseInt(_day) +"天前";
else if(_hour>=1) result.text=parseInt(_hour) +"个小时前";
else if(_min>=1) result.text=parseInt(_min) +"分钟前";
else result.text="刚刚";
return result;
}
//此方法来源于 https://github.com/vuejs/vue-hackernews/blob/gh-pages/src/filters/index.js
function fromNow (dateStr) {
const between = Date.now() / 1000 - Number(new Date(dateStr.replace(/-/g,‘/‘)).getTime()) / 1000;
if (between < 3600) {
return (~~(between / 60)+‘分钟前‘);
} else if (between < 86400) {
return (~~(between / 3600)+‘小时前‘);
} else {
return (~~(between / 86400)+‘天前‘);
}
}
function isToday(dateStr){
return (new Date().toDateString()==(new Date(dateStr.replace(/-/g,‘/‘)).toDateString()));
}
以上是关于JS代码片段:一个日期离现在多久了的主要内容,如果未能解决你的问题,请参考以下文章
sql 日期转换代码片段 - Dato,120,konvertere
如何从片段中调用 getSupportFragmentManager()?