Open In App

Node.js Date.addHours() API

Last Updated : 12 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The date-and-time.Date.addHours() is a minimalist collection of functions for manipulating JS date and time module which is used to add the extra Hours to the existing date and time.

Required Module: Install the module by npm or used it locally.

  • By using npm.
npm install date-and-time --save
  • By using CDN link.
<script src="/path/to/date-and-time.min.js"></script>

Syntax:

addHours(dateObj, months)

Parameters: This method takes the following arguments as a parameter:

  • dateobject: It is the string object of the date.
  • hours: It is the number of hours.

Return Value: This method returns updated date and time.

Example 1:

index.js




// Node.js program to demonstrate the  
// Date.addHours() method
  
// Importing module
const date = require('date-and-time')
  
// Creating object of current date and time 
// by using Date() 
const now  =  new Date();
  
// Adding Hours to the existing date and time
// by using date.addHours() method
const value = date.addHours(now,24);
  
// Display the result
console.log("updated date and time : " + value)


Run the index.js file using the following command:

node index.js

Output:

updated date and time :
Mon Mar 08 2021 20:35:37 GMT+0530 (India Standard Time)

Example 2:

index.js




// Node.js program to demonstrate the  
// Date.addHours() method
  
// Importing module
const date = require('date-and-time')
  
// Creating object of current date and time 
// by using Date() 
const now = new Date();
  
now.setDate(20)
  
// Adding Hours to the existing date and time
// by using date.addHours() method
const value = date.addHours(now, 30);
  
// Display the result
console.log("updated date and time : " + value)


Run the index.js file using the following command:

node index.js

Output:

updated date and time :
Mon Mar 22 2021 02:37:29 GMT+0530 (India Standard Time)

Reference: https://github.com/knowledgecode/date-and-time#addhoursdateobj-hours



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads