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

Js 日期 多少分钟前,多少秒前

时间:2016-01-13 21:52:44      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

;(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);
dateDiff(new Date(‘1987-04-03‘).getTime()) //一定要转换成时间戳

  

Js 日期 多少分钟前,多少秒前

标签:

原文地址:http://www.cnblogs.com/szatpig/p/5128442.html

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