📜  Node.js Date.addHours() API(1)

📅  最后修改于: 2023-12-03 14:44:38.230000             🧑  作者: Mango

Node.js Date.addHours() API

The Date.addHours() API in Node.js is a method used to add a specified number of hours to a given date object.

Syntax
dateObject.addHours(hours)
  • dateObject: The date object to which the hours are to be added.
  • hours: The number of hours to be added to the date object.
Example Usage
const now = new Date();
const inTwoHours = now.addHours(2);

console.log(now); // Output: Current Date
console.log(inTwoHours); // Output: Current Date + 2 Hours
Explanation

In the above example, the now object is created using the Date constructor with no arguments which by default creates a Date object representing the current date and time. The addHours() method is then called on the now object with an argument of 2 which adds 2 hours to the current date and returns a new Date object representing the future date/time.

Finally, the console.log() method is used to output both the now and inTwoHours objects to the console, demonstrating the difference in time between the two dates.

Conclusion

The Date.addHours() API in Node.js is a simple and convenient method for adding a specified number of hours to a date object. It can be useful in various scenarios where date/time manipulation is required.