Add time to string data/time - JavaScript?

At first, set a new Date in JavaScript −

var dateValue = new Date("2021-01-12 10:10:20");

Use new Date() along with setHours() and getHours() to add time.


Following is the code −

var dateValue = new Date("2021-01-12 10:10:20");
dateValue.setHours(dateValue.getHours() + 2);
console.log("The date value is=" + dateValue.toString());
console.log("Only Hours value after incrementing=" +

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo291.js.


This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo291.js
The date value is=Tue Jan 12 2021 12:10:20 GMT+0530 (India Standard Time)
Only Hours value after incrementing=12