Open In App

Moment.js moment().subtract() Method

Last Updated : 26 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().subtract() method is used to subtract the given unit of time from the Moment object. The unit can be specified in all the recognized variations of the unit including its plural and short forms.

Syntax:

moment().subtract(Number, String)
OR
moment().subtract(Duration)
OR
moment().subtract(Object)

Parameters: This method accepts multiple parameters as mentioned above and described below:

  • Number: It is a Number that denotes the time value that has to subtracted.
  • String: It is a string that denotes the unit of time that has to be subtracted.
  • Duration: This is a Duration object that contains the time that needs to be subtracted.
  • Object: It is an object that can be used to denote all the time values that can be subtracted to the Moment.

Return Value: This method returns a string of the given unit of time from the Moment 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().subtract() Method.

Example 1:

Javascript




const moment = require('moment');
  
let momentA = moment();
console.log(
    "Current MomentA is:", momentA.toString()
);
  
momentA.subtract(5, 'hours');
console.log(
    "Current MomentA is:", momentA.toString()
);
  
momentA.subtract(15, 'minutes');
console.log(
    "Current MomentA is:", momentA.toString());
  
momentA.subtract(2, 'days');
console.log(
    "Current MomentA is:", momentA.toString()
);
  
momentA.subtract(2, 'months');
console.log(
    "Current MomentA is:", momentA.toString()
);
  
momentA.subtract(10, 'years');
console.log(
    "Current MomentA is:", momentA.toString()
);


Output:

Current MomentA is: Tue Jul 26 2022 01:24:42 GMT+0530
Current MomentA is: Tue Jul 26 2022 11:24:42 GMT+0530
Current MomentA is: Tue Jul 26 2022 12:09:42 GMT+0530
Current MomentA is: Sun Jul 31 2022 12:09:42 GMT+0530
Current MomentA is: Tue Jan 31 2023 12:09:42 GMT+0530
Current MomentA is: Fri Jan 31 2025 12:09:42 GMT+0530

Example 2:

Javascript




const moment = require('moment');
  
let momentB = moment();
console.log(
    "Current MomentB is:", momentB.toString()
);
  
momentB.subtract({ hours: 10, minutes: 50, seconds: 25 });
console.log(
    "Current MomentB is:", momentB.toString()
);
  
let momentC = moment();
console.log(
    "Current MomentC is:", momentC.toString()
);
  
momentC.subtract({ days: 10, months: 15, years: 2 });
console.log(
    "Current MomentC is:", momentC.toString()
);


Output:

Current MomentB is: Tue Jul 26 2022 01:24:42 GMT+0530
Current MomentB is: Tue Jul 26 2022 06:35:12 GMT+0530
Current MomentC is: Tue Jul 26 2022 01:24:42 GMT+0530
Current MomentC is: Mon May 31 2038 01:24:42 GMT+0530

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



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

Similar Reads