Adding hours to JavaScript Date object?

前端 未结 16 1199
一生所求
一生所求 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条回答
  •  旧时难觅i
    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;
    }
    

提交回复
热议问题