
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
Example
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=" + dateValue.getHours());
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo291.js.
Output
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
- Related Articles
- Rectifying the time string in JavaScript
- How to add time in minutes in datetime string PHP?
- How to Convert Time String to Time in Excel?
- Convert string of time to time object in Java
- Calculate total time duration (add time) in MySQL?
- How to set current time to some other time in JavaScript?
- Add 2 hours to current time in MySQL?
- Forming the nearest time using current time in JavaScript
- How to convert UTC date time into local date time using JavaScript?
- How to convert string to time in MySQL?
- How to add a date and time in HTML5?
- How to add 5 hours to current time in MySQL?
- Generating time data in SAP HANA
- Amortized time complexity in Data Structures
- Add hours to current time using Calendar.add() method in Java

Advertisements