Adding hours in JavaScript date object

In this quick javascript tutorial we’ll learn how to add hours in default javascript data object. you can easily set hours to data object using JavaScript setHours() method. Same JavaScript getHours() method will returns the hour (from 0 to 23) of the specified date and time. By using these two methods setHours() and getHours(), you can easily add hours to Date object.

Use below function to add hours to current date, Here used javaScript prototype method to inherit properties in data object.

Date.prototype.addHours= function(h){
    return this;

Pass the hours as a parameter in addHours() function which you want to add to the Date object. See below example add 3 hours to current date.

console.log(new Date().addHours(3));
alert(new Date().addHours(3));

Now you’ll see 3 hours will be added in current date.
If you want to apply more customisation on javascript data object you must try plugin called datejs which help you play with javascript data object.

Thanks 🙂