Open In App

Node.js Date.addYears() API

Improve
Improve
Like Article
Like
Save
Share
Report

The date-and-time.Date.addYears() is a minimalist collection of functions for manipulating JS date and time module which is used to add the extra years 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:

const addYears(dateObj, years)

Parameters: This API takes the following arguments as parameters.

  • dateobject: The string object of the date.
  • years: Number of years.

Return Value: This method returns the updated date and time.

Example 1:

index.js




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


Run index.js file using below command:

node index.js

Output:

updated date and time : 
Tue Mar 07 2045 19:48:00 GMT+0530 (India Standard Time)

Example 2:

index.js




// Node.js program to demonstrate the  
// Date.addYears() APi
  
// Importing http 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 years to the existing date and time
// by using date.addYears() api
const value = date.addYears(now,30);
  
// Display the result
console.log("updated date and time : " + value)


Run index.js file using below command:

node index.js

Output:

updated date and time : 
Mon Mar 20 2051 19:50:44 GMT+0530 (India Standard Time)

Reference: https://github.com/knowledgecode/date-and-time#addyearsdateobj-years



Last Updated : 09 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads