. Advertisement .
..3..
. Advertisement .
..4..
JavaScript is a programming language or scripting that enables you to execute complex attributes on web pages. Numerous websites utilize the lightweight, object-oriented programming language JavaScript (js) to script their web pages. If you are confused with the problem of How to add hours to a Date in JavaScript, let’s read our article. It’s beneficial for you.
What Can We Do To Add Hours To A Date In JavaScript?
JavaScript’s Date object shows you the current week, date, and time on your system (with timezone). Although UTC is the Date object’s default timezone, the local (system’s) timezone is used to retrieve the date and time or any components.
You can access or change the values of the Date object, such as time, timezone, hours, minutes,.…, by using many methods provided by the Date object, such as setHours(), getTime(), setMonth(), getDay(), etc.
Now, let’s follow these solutions we mention below to add hours to a date in JavaScript.
Solution 1: Utilize the setHours() method
In JavaScript, you can add hours to a date by using the setHours() method. However, the minutes and seconds won’t be updated by this solution; it only updates the value of hours.
Now let’s look at the following instance and see how the setHours() method can be used to change the value of the hours. To begin with, you must use the new keyword to construct an object of the Date, put it in a variable called currentDateTime, and display that variable then to obtain the system’s current time. Then, you will be provided your system’s local time.
Example:
var currentDateTime = new Date();
function addHours(hr){
currentDateTime.setHours(hr);
}
console.log(currentDateTime);
addHours(5);
console.log(currentDateTime);
Result:
Mon May 5 2022 00:28:11 GMT+0530 (India Standard Time)
Mon May 5 2022 05:28:11 GMT+0530 (India Standard Time)
Solution 2: Utilize the setTime and getTime method
You also can utilize the setTime and getTime method to convert a comma-separated string to array in JavaScript. Look at the following example:
const date = new Date(2021, 1, 1)
const h = 2
date.setTime(date.getTime() + (h * 60 * 60 * 1000))
console.log(date)
You will have an object called date with a date to which you wish to add hours. The amount of hours you want to add will be in h
. Then, you call setTime with the millisecond-accurate timestamp of the new date.
By writing the following, you can have the timestamp of the date plus two hours.
date.getTime() + (h * 60 * 60 * 1000)
2 hours with the millisecond-accurate is (h * 60 * 60 * 1000)
Now, the date you can get is:
Mon Feb 01 2021 02:00:00 GMT-0800 (Pacific Standard Time)
Solution 3: Utilize moment.js
Utilizing moment.js to add hours to a Date in JavaScript also is a great solution. For example:
const newDate = new moment(new Date(2021, 1, 1)).add(10, 'hours').toDate();
console.log(newDate)
In the above example, we pass into the moment function to add 10 houThe quantity to add is the 1st argument. The unit of the quantity that needs to add is the 2nd argument. Returned the moment object with the updated date-time was converted to a native JavaScript date object by the function toDate.
Therefore, newDate is displayed in the console log as shown below:
Mon Feb 01 2021 10:00:00 GMT-0800 (Pacific Standard Time)
Conclusion
The above solutions are the best ways for you to add hours to a Date in JavaScript. We believe they are helpful for you. Please comment below if you have any questions or problems. We are always willing to help you. Thank you for your reading!
Read more
→ How To Convert a Date string to ISO format using JavaScript
Leave a comment