Adding hours to JavaScript Date object?

前端 未结 16 1165
一生所求
一生所求 2020-11-22 09:00

It amazes me that JavaScript\'s Date object does not implement an add function of any kind.

I simply want a function that can do this:

var now = Date         


        
相关标签:
16条回答
  • 2020-11-22 09:56

    Check if its not already defined, otherwise defines it on the Date prototype:

    if (!Date.prototype.addHours) {
        Date.prototype.addHours = function(h) {
            this.setHours(this.getHours() + h);
            return this;
        };
    }
    
    0 讨论(0)
  • 2020-11-22 09:58

    For a simple add/subtract hour/minute function in javascript, try this:

    function getTime (addHour, addMin){
        addHour = (addHour?addHour:0);
        addMin = (addMin?addMin:0);
        var time = new Date(new Date().getTime());
        var AM = true;
        var ndble = 0;
        var hours, newHour, overHour, newMin, overMin;
        //change form 24 to 12 hour clock
        if(time.getHours() >= 13){
            hours = time.getHours() - 12;
            AM = (hours>=12?true:false);
        }else{
            hours = time.getHours();
            AM = (hours>=12?false:true);
        }
        //get the current minutes
        var minutes = time.getMinutes();
        // set minute
        if((minutes+addMin) >= 60 || (minutes+addMin)<0){
            overMin = (minutes+addMin)%60;
            overHour = Math.floor((minutes+addMin-Math.abs(overMin))/60);
            if(overMin<0){
                overMin = overMin+60;
                overHour = overHour-Math.floor(overMin/60);
            }
            newMin = String((overMin<10?'0':'')+overMin);
            addHour = addHour+overHour;
        }else{
            newMin = minutes+addMin;
            newMin = String((newMin<10?'0':'')+newMin);
        }
        //set hour
        if(( hours+addHour>=13 )||( hours+addHour<=0 )){
            overHour = (hours+addHour)%12;
            ndble = Math.floor(Math.abs((hours+addHour)/12));
            if(overHour<=0){
                newHour = overHour+12;
                if(overHour == 0){
                    ndble++;
                }
            }else{
                if(overHour ==0 ){
                    newHour = 12;
                    ndble++;
                }else{
                    ndble++;
                    newHour = overHour;
                }
            }
            newHour = (newHour<10?'0':'')+String(newHour);
            AM = ((ndble+1)%2===0)?AM:!AM;
        }else{
            AM = (hours+addHour==12?!AM:AM);
            newHour = String((Number(hours)+addHour<10?'0':'')+(hours+addHour));
        }
        var am = (AM)?'AM':'PM';
        return new Array(newHour, newMin, am);
    };
    

    This can be used without parameters to get the current time

    getTime();
    

    or with parameters to get the time with the added minutes/hours

    getTime(1,30); // adds 1.5 hours to current time
    getTime(2);    // adds 2 hours to current time
    getTime(0,120); // same as above
    

    even negative time works

    getTime(-1, -30); // subtracts 1.5 hours from current time
    

    this function returns an array of

    array([Hour], [Minute], [Meridian])
    
    0 讨论(0)
  • 2020-11-22 09:59

    I also think the original object should not be modified. So to save future manpower here's a combined solution based on Jason Harwig's and Tahir Hasan answers:

    Date.prototype.addHours= function(h){
        var copiedDate = new Date();
        copiedDate.setTime(this.getTime() + (h*60*60*1000)); 
        return copiedDate;
    }
    
    0 讨论(0)
  • 2020-11-22 10:00

    If someone is still looking at this issue, the easiest way to do is

    var d = new Date();
    d.setHours(15);
    
    0 讨论(0)
提交回复
热议问题