Open In App

Moment.js moment.duration().add() Method

Last Updated : 18 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().duration().add() method is used to add a given time to the duration. The time to be added can be another Duration object, value in milliseconds, or a representation of the time using a string and numerical value. All the shorthands and keys used for creating durations can be used here for the time. 

Syntax:

moment().duration().add(Number) // or
moment().duration().add(Duration) // or
moment().duration().add(Object) // or
moment().duration().add(Number, String)

Parameters: This method accepts a single parameter that denotes the time to be added. The available formats can be:

  • Number: This will be the number of milliseconds denoting the time that will be added.
  • Duration: This denotes another Duration object that will be added.
  • Object: This denotes the time to be added as an object.
  • Number, String: This denotes the time to be added denoted using a number value and its type as a string value.

Return Value: It returns the modified Duration object.

Note: This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

The below examples will demonstrate the Moment.js moment.duration().add() Method.

Example 1:

Javascript




const moment = require('moment');
  
let durationOne = moment.duration(9, 'months');
console.log(
    "Data of durationOne before adding:",
    durationOne._data
)
console.log(
    "Humanized Data of durationOne before adding:",
    durationOne.humanize()
)
  
// Adding using add(Number, String)
durationOne.add(600, 'days');
console.log(
    "Data of durationOne after adding:",
    durationOne._data
)
console.log(
    "Humanized Data of durationOne after adding:",
    durationOne.humanize()
)
  
let durationTwo = moment.duration(5, 'seconds');
console.log(
    "Data of durationTwo before adding:"
    durationTwo._data
)
console.log(
    "Humanized Data of durationTwo before adding:",
    durationTwo.humanize()
)
  
// Adding using add(Number)
durationTwo.add(2050);
console.log(
    "Data of durationTwo after adding:",
    durationTwo._data
)
console.log(
    "Humanized Data of durationTwo after adding:",
    durationTwo.humanize()
)


Output:

 Data of durationOne before adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 0,
  days: 0,
  months: 9,
  years: 0
}
Humanized Data of durationOne before adding: 9 months
Data of durationOne after adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 0,
  days: 21,
  months: 4,
  years: 2
}
Humanized Data of durationOne after adding: 2 years
Data of durationTwo before adding: {
  milliseconds: 0,
  seconds: 5,
  minutes: 0,
  hours: 0,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationTwo before adding: a few seconds
Data of durationTwo after adding: {
  milliseconds: 50,
  seconds: 7,
  minutes: 0,
  hours: 0,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationTwo after adding: a few seconds

Example 2:

Javascript




const moment = require('moment');
  
let durationThree = moment.duration(15, 'hours');
console.log(
    "Data of durationThree before adding:",
    durationThree._data
)
console.log(
    "Humanized Data of durationThree before adding:",
    durationThree.humanize()
)
  
let durationToAdd = moment.duration(5, 'days');
  
// Adding using add(Duration)
durationThree.add(durationToAdd);
console.log(
    "Data of durationThree after adding:",
    durationThree._data
)
console.log(
    "Humanized Data of durationThree after adding:",
    durationThree.humanize()
)
  
let durationFour = moment.duration(4, 'hours');
console.log(
    "Data of durationFour before adding:",
    durationFour._data
)
console.log(
    "Humanized Data of durationFour before adding:",
    durationFour.humanize()
)
  
// Adding using add(Object)
let durationToAddAgain = moment.duration({months: 2, days: 16});
  
durationFour.add(durationToAddAgain);
console.log(
    "Data of durationFour after adding:",
    durationFour._data
)
console.log(
    "Humanized Data of durationFour after adding:",
    durationFour.humanize()
)


Output:

Data of durationThree before adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 15,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationThree before adding: 15 hours
Data of durationThree after adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 15,
  days: 5,
  months: 0,
  years: 0
}
Humanized Data of durationThree after adding: 6 days
Data of durationFour before adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 4,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationFour before adding: 4 hours
Data of durationFour after adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 4,
  days: 16,
  months: 2,
  years: 0
}
Humanized Data of durationFour after adding: 3 months

Reference: https://momentjs.com/docs/#/durations/add/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads