Sometimes, we want to add hours to a parsed moment date with JavaScript.
In this article, we’ll look at how to add hours to a parsed moment date with JavaScript.
How to add hours to a parsed moment date with JavaScript?
To add hours to a parsed moment date with JavaScript, we can use the add
method.
For instance, we write:
const myDate = "2022-04-14 07:07:15";
const newDate = moment(myDate).add(5, 'hours').format('YYYY-MM-DD hh:mm:ss');
console.log(newDate)
to add 5 hours to the moment object parsed from myDate
.
We call add
with 5 and 'hours'
to add 5 hours.
And then we call format
with a format string to return the datetime stored in the moment object.
Therefore, newDate
is "2022-04-14 12:07:15"
.
Conclusion
To add hours to a parsed moment date with JavaScript, we can use the add
method.