Open In App

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

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

The moment().duration().subtract() method is used to subtract the given time from a duration. The time to subtract 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().subtract(Number)
moment().duration().subtract(Duration)
moment().duration().subtract(Object)
moment().duration().subtract(Number, String)

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

  • Number: This will be the number of milliseconds denoting the time that will be subtracted.
  • Duration: This denotes another Duration object that will be subtracted.
  • Object: This denotes the time to be subtracted as an object.
  • Number, String: This denotes the time to be subtracted 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

Example 1: The below examples will demonstrate the Moment.js moment.duration().subtract() Method.

Javascript




const moment = require('moment');
  
// Example 1
  
let durationOne = moment.duration(4, 'years');
console.log(
    "Data of durationOne before subtracting:",
    durationOne._data
)
console.log(
    "Humanized Data of durationOne before subtracting:",
    durationOne.humanize()
)
  
// Subtracting using subtract(Number, String)
durationOne.subtract(24, 'months');
console.log(
    "Data of durationOne after subtracting:",
    durationOne._data
)
console.log(
    "Humanized Data of durationOne after subtracting:",
    durationOne.humanize()
)
  
let durationTwo = moment.duration(130, 'seconds');
console.log(
    "Data of durationTwo before subtracting:",
    durationTwo._data
)
console.log(
    "Humanized Data of durationTwo before subtracting:",
    durationTwo.humanize()
)
  
// Subtracting using subtract(Number)
durationTwo.subtract(50000);
console.log(
    "Data of durationTwo after subtracting:",
    durationTwo._data
)
console.log(
    "Humanized Data of durationTwo after subtracting:",
    durationTwo.humanize()
)


Steps to run the application: Write the below command in the terminal to run the index.js file:

node index.js

Output:

Data of durationOne before subtracting: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 0,
  days: 0,
  months: 0,
  years: 4
}
Humanized Data of durationOne before subtracting: 4 years
Data of durationOne after subtracting: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 0,
  days: 0,
  months: 0,
  years: 2
}
Humanized Data of durationOne after subtracting: 2 years 
Data of durationTwo before subtracting: {
  milliseconds: 0,
  seconds: 10,
  minutes: 2,
  hours: 0,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationTwo before subtracting: 2 minutes
Data of durationTwo after subtracting: {
  milliseconds: 0,
  seconds: 20,
  minutes: 1,
  hours: 0,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationTwo after subtracting: a minute

Example 2:

Javascript




const moment = require('moment');
  
let durationThree = moment.duration(15, 'hours');
console.log(
    "Data of durationThree before subtracting:",
    durationThree._data
)
console.log(
    "Humanized Data of durationThree before subtracting:",
    durationThree.humanize()
)
  
// Subtracting using subtract(Duration)
let durationTosubtract = moment.duration(72, 'minutes');
  
durationThree.subtract(durationTosubtract);
console.log(
    "Data of durationThree after subtracting:",
    durationThree._data
)
console.log(
    "Humanized Data of durationThree after subtracting:",
    durationThree.humanize()
)
  
let durationFour = moment.duration(4, 'hours');
console.log(
    "Data of durationFour before subtracting:",
    durationFour._data
)
console.log(
    "Humanized Data of durationFour before subtracting:",
    durationFour.humanize()
)
  
// Subtracting using subtract(Object)
let durationTosubtractAgain = moment.duration(
    { minutes: 120, seconds: 30 }
);
  
durationFour.subtract(durationTosubtractAgain);
console.log(
    "Data of durationFour after subtracting:",
    durationFour._data
)
console.log(
    "Humanized Data of durationFour after subtracting:",
    durationFour.humanize()
)


Output:

Data of durationThree before subtracting: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 15,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationThree before subtracting: 15 hours
Data of durationThree after subtracting: {
  milliseconds: 0,
  seconds: 0,
  minutes: 48,
  hours: 13,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationThree after subtracting: 14 hours
Data of durationFour before subtracting: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 4,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationFour before subtracting: 4 hours
Data of durationFour after subtracting: {
  milliseconds: 0,
  seconds: 30,
  minutes: 59,
  hours: 1,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationFour after subtracting: 2 hours

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



Like Article
Suggest improvement
Share your thoughts in the comments