How to add 24 hours to datetime object of JavaScript?

Sometimes, we want to add 24 hours to datetime object of JavaScript.

In this article, we’ll look at how to add 24 hours to datetime object of JavaScript.

How to add 24 hours to datetime object of JavaScript?

To add 24 hours to datetime object of JavaScript, we can use getTime to get the timestamp of the original datetime.

And then we add 60 * 60 * 24 * 1000 to it to add 24 hours to it.

Then we can use the Date constructor with the timestamp to create a new datetime with the 24 hours added.

For instance, we write:

const d = new Date(2022, 1, 1, 1)
const newD = new Date(d.getTime() + 60 * 60 * 24 * 1000);
console.log(d)

to create the datetime d.

Then we call d.getTime to get timestamp from d in milliseconds.

Next, we add 60 * 60 * 24 * 1000 which is 24 hours in milliseconds to the timestamp.

And then we use the Date constructor with the return timestamp and assign it to newD.

As a result, newD is Tue Feb 01 2022 01:00:00 GMT-0800 (Pacific Standard Time).

Conclusion

To add 24 hours to datetime object of JavaScript, we can use getTime to get the timestamp of the original datetime.

And then we add 60 * 60 * 24 * 1000 to it to add 24 hours to it.

Then we can use the Date constructor with the timestamp to create a new datetime with the 24 hours added.