JavaScript itself has terrible Date/Time API's. Nonetheless, you can do this in pure JavaScript:
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
Answer from Jason Harwig on Stack Overflow Top answer 1 of 16
699
JavaScript itself has terrible Date/Time API's. Nonetheless, you can do this in pure JavaScript:
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
2 of 16
417
Date.prototype.addHours= function(h){
this.setHours(this.getHours()+h);
return this;
}
Test:
alert(new Date().addHours(4));
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › setHours
Date.prototype.setHours() - JavaScript | MDN
The setHours() method of Date instances changes the hours, minutes, seconds, and/or milliseconds for this date according to local time. const event = new Date("August 19, 1975 23:15:30"); event.setHours(20); console.log(event); // Expected output: "Tue Aug 19 1975 20:15:30 GMT+0200 (CEST)" ...
Futurestud.io
futurestud.io › tutorials › add-hours-to-a-date-in-javascript-or-node-js
Add Hours to a Date in JavaScript or Node.js
The getHours returns the current number of hours of a given date. And the setHours method changes the hour of a given date instance. The nice thing here is that JavaScript takes care of changing the day/month/year in case the added hours shift something of that.
Readthedocs
momentjscom.readthedocs.io › en › latest › moment › 03-manipulating › 01-add
Add - momentjs.com
If you are adding hours, minutes, seconds, or milliseconds, the assumption is that you want precision to the hour, and will result in a different hour. var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US m.hours(); // 5 m.add(24, 'hours').hours(); // 6 (but you may have to set the timezone first)
TutorialsPoint
tutorialspoint.com › How-to-add-2-hours-to-a-JavaScript-Date-object
How to add 2 hours to a JavaScript Date object?
To add 2 hours into the Date object using the setHours() method first we get the value of hours of the current time and then add 2 into it and pass the added value to the setHours() method.
W3Schools
w3schools.com › jsref › jsref_sethours.asp
JavaScript Date setHours() Method
cssText getPropertyPriority() ... · const d = new Date("2025-01-15"); d.setHours(15); Try it Yourself » · setHours() sets the hour of a date....
Envato Tuts+
webdesign.tutsplus.com › home › web design › html/css › javascript for designers
How to Add and Subtract Time From a Date in JavaScript | Envato Tuts+
September 19, 2023 - Basically, we’ve just multiplied the number of minutes in an hour (60) by the number of seconds in a minute (60) by the number of milliseconds in a second (1000) to get the number of milliseconds in an hour. As you might already know, you can initialize a Date object by providing the number of milliseconds as the first argument, and this would initialize a date object in reference to it. Thus, we’ve added numberOfMlSeconds and addMlSeconds to get the total number of milliseconds, and we’ve used it to initialize a new date object.
TutorialsPoint
tutorialspoint.com › article › How-to-add-hours-and-minutes-to-a-date-with-JavaScript
How to add hours and minutes to a date with JavaScript?
Add hours or minutes with the getTime() Method. JavaScript date setHours() method sets the hours for a specified date according to local time.
Day.js
day.js.org › docs › en › manipulate › add
Add · Day.js
result = dayjs().add(dayjs.duration({'days' : 1}))
W3Schools
w3schools.com › js › js_date_methods_set.asp
JavaScript Date Set Methods
If adding days shifts the month or year, the changes are handled automatically by the Date object. The setHours() method sets the hours of a date object (0-23):