js 日期多少小时前、多少分钟前、多少秒前

项目示例 https://github.com/jaywcjlove/date.js

源码:

;(function(window){
    /**
     * [dateDiff 算时间差]
     * @param  {[type=Number]} hisTime [历史时间戳,必传]
     * @param  {[type=Number]} nowTime [当前时间戳,不传将获取当前时间戳]
     * @return {[string]}         [string]
     */
    var dateDiff = function(hisTime,nowTime){
        var now =nowTime?nowTime:new Date().getTime(),
            diffValue = now - hisTime,
            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;
            
            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;
    }
    window.dateDiff = dateDiff
})(window);

示例:

例子:
dateDiff(Timestamp,now Timestamp)
Timestamp:毫秒


dateDiff(1411430400000,1421313395359)
//=>"3个月前"

dateDiff(new Date('1987-04-03').getTime())
//=>"28年前"
改成了型参数,应该是这样吧
;(function(window){
    /**
     * [dateDiff 算时间差]
     * @param  {[type=Number]} hisTime [历史时间戳,必传]
     * @param  {[type=Number]} nowTime [当前时间戳,不传将获取当前时间戳]
     * @return {[string]}         [string]
     */
    var dateDiff = function(hisTime,nowTime){
        if(!arguments.length) return '';
        var arg = arguments,
            now =arg[1]?arg[1]:new Date().getTime(),
            diffValue = now - arg[0],
            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;

            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;
    }
    window.dateDiff = dateDiff
})(window);

供参考!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值