jq 时间计算

时间差计算:

-(function($){
        var caculation = function(gap){
            var minutes = 1000 * 60
            var hours = minutes * 60
            var days = hours * 24
            var years = days * 365
            if(gap/years >= 1){
                return parseInt(gap/years)+'年';
            }else if(gap/days >= 30){
                return parseInt(gap/days/30)+'个月'
            }else if(gap/hours >= 24){
                return parseInt(gap/hours/24)+'天'
            }else if(gap/minutes >=60){
                var num = gap/minutes/60;
                var intNum = parseInt(gap/minutes/60);
                return intNum+'小时'+parseInt((num.toFixed(4)-intNum)*60)+'分钟'
            }else{
                return parseInt(gap/minutes)+'分钟'
            }
        }
        $.extend({
            nowToTime:function(time){
                var theTime = Date.parse(time .replace(/-/g,"/"));
                var curDate=new  Date().valueOf();//当前时间的时间戳
                if(theTime <=curDate){
                    alert("请选择大于今天的时间!");
                }else{
                    var gap = theTime - curDate;
                    return caculation(gap)
                }
            },
            timeToNow:function(time){
                var theTime = Date.parse(time .replace(/-/g,"/"));
                var curDate=new  Date().valueOf();
                if(theTime <=curDate){
                    var gap =  curDate - theTime;
                    return caculation(gap)
                }else{
                    alert("请选择小于今天的时间!");
                }
            },
            timeToTime:function(timeOne,timeTwo){
                if(timeOne == undefined && timeTwo == undefined){
                    alert('请输入日期');
                    return false;
                }
                var timeOne = timeOne == undefined ?
                new  Date().valueOf():
                Date.parse(timeOne .replace(/-/g,"/"));
                var timeTwo = timeTwo == undefined ?
                new  Date().valueOf():
                Date.parse(timeTwo.replace(/-/g,"/"));
                var gap = timeOne > timeTwo?
                timeOne-timeTwo:
                timeTwo-timeOne;
                return caculation(gap)
            
            }
        })
    })(jQuery);
    console.log($.nowToTime('2017-7-16 17:51:34')+'后');
    console.log($.timeToNow('2017-07-15 18:01:23')+'前');
    console.log($.timeToTime('2017-07-15 18:01:23','2018-06-16 19:02:23'))

倒计时:

;(function() {

    var Grewer = {
        init:function(option,obj){
              var trigger = option.trigger || 's',
                limit = (option.limit === 'over') ? '1' : (option.limit === 'inadequate') ? '-1' : '0',
                time = option.time;
            console.log(trigger, limit, time)
              
            var now = (new Date()).valueOf();
            var gap = now - (new Date(time)).valueOf();
         
            switch (limit) {
                case '0':
                        this.addHtml(obj, gap,trigger,limit);
                    break;
                case '1':
                    if (gap < 0) {
                        this.addHtml(obj, gap, trigger,limit);

                    } else {
                        console.error('未超过当前时间')
                        alert('未超过当前时间')
                    }
                    break;
                case '-1':
                    if (gap >= 0) {
                        this.addHtml(obj, gap,trigger, limit);

                    } else {
                        console.error('超过了当前时间')
                        alert('超过了当前时间');
                    }
            }
        },
        addHtml:function(obj,gap,trigger){
            this.caculation(gap)
           

            var html = (trigger === 's') ? '<div class="GrewerTime"><span class="hours">' + this.intHours + '</span>小时<span class="minutes">' + this.intMinutes + '</span>分<span class="second">' + this.intSeconds + '</span>秒</div>' :
            '<div class="GrewerTime"><span class="hours">' + this.intHours + '</span>小时<span class="minutes">' + this.intMinutes+ '</span>分</div>';

            obj.append(html);

            (trigger === 's') ? this.addTrigger(obj) : this.addHourTrigger(obj);
        },
        caculation:function(gap){
            gap = Math.abs(gap);
            var minutes = 1000 * 60,
                hours = minutes * 60,
                days = hours * 24

            this.intHours = (gap / hours) | 0;
            this.intMinutes = ((gap - this.intHours * hours) / minutes) | 0;
            this.intSeconds = ((gap - this.intMinutes * minutes - this.intHours * hours) / 1000) | 0;
        },
        addTrigger:function(obj){
            setTimeout(function() {
            var hours = obj.find('.hours').text(),
                minutes = obj.find('.minutes').text(),
                second = obj.find('.second').text();

            if (second <= 0) {
                if (minutes <= 0) {
                    if (hours <= 0) {
                        return false;
                    }
                    this.minus(obj.find('.hours'));
                    obj.find('.second').text(59);
                    obj.find('.minutes').text(59);
                } else {
                    this.minus(obj.find('.minutes'));
                    obj.find('.second').text(59);
                }
                return this.addTrigger(obj);
            }

            this.minus(obj.find('.second'));
            this.addTrigger(obj);

        }.bind(this), 1000);
        },
        addHourTrigger:function(obj){

        setTimeout(function() {
            var hours = obj.find('.hours').text(),
                minutes = obj.find('.minutes').text()

            
                if (minutes <= 0) {
                    if (hours <= 0) {
                        return false;
                    }
                    this.minus(obj.find('.hours'));
                    obj.find('.minutes').text(59);
                    return this.addHourTrigger(obj);
                }

                this.minus(obj.find('.minutes'));
                this.addHourTrigger(obj);
             ;

        }.bind(this), 1000*60);
        },
        minus:function(obj){
            var bar = (obj.text() | 0) - 1
            obj.text(bar);
            return bar;
        }
    }


  


    $.fn.extend({
        timeTrigger: function(option) {
            //1 this 是单个
            //option参数
            // trigger 触发时分秒; s,m
            // time  时间;
            // limit (over  inadequate) 是否接受某个参数必须大于或小于当前时间
            // (0=underfined:没有限制)(1=over:大于当前时间)(-1=inadequate:小于当前时间);
            if (option.time === void 0) {
                alert('请输入时间');
                return false;
            }
              var G = Object.create(Grewer);
              G.init(option,this);

        }
    })


}());

$('#time').timeTrigger({
    time: '2018-09-9 15:12:00',
    trigger:'s',
});

 

posted @ 2017-07-15 18:12  Grewer  阅读(1978)  评论(0编辑  收藏  举报