How to add N hours to date object in JavaScript

3 Answers

0 votes
Date.prototype.addHours = function(hours) {
    this.setHours(this.getHours()+ hours);
    return this;
};


document.write(new Date().addHours(2));

 
/*
run:
 
Tue Sep 17 2019 11:48:01 GMT+0300 (Israel Daylight Time) 
    
*/

 



answered Sep 17, 2019 by avibootz
0 votes
Date.prototype.addHours = function(hours) {
    this.setHours(this.getHours()+ hours);
    return this;
};

var dt = new Date().addHours(2);
document.write(dt);

 
/*
run:
 
Tue Sep 17 2019 11:51:39 GMT+0300 (Israel Daylight Time) 
    
*/

 



answered Sep 17, 2019 by avibootz
0 votes
var dt = new Date();

dt.setHours(dt.getHours() + 3);
document.write(dt);


 
/*
run:
 
Tue Sep 17 2019 12:52:49 GMT+0300 (Israel Daylight Time) 
    
*/

 



answered Sep 17, 2019 by avibootz

Related questions

...