Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  13] [ 2]  / answers: 1 / hits: 162895  / 8 Years ago, fri, january 6, 2017, 12:00:00

I have a current month generated using moment().format('MMM YYYY'). I need to get last six months using this current month.



How to subtract one month using moment.js?


More From » momentjs

 Answers
59

For substracting in moment.js:



moment().subtract(1, 'months').format('MMM YYYY');


Documentation:



http://momentjs.com/docs/#/manipulating/subtract/




Before version 2.8.0, the moment#subtract(String, Number) syntax was
also supported. It has been deprecated in favor of
moment#subtract(Number, String).




  moment().subtract('seconds', 1); // Deprecated in 2.8.0
moment().subtract(1, 'seconds');



As of 2.12.0 when decimal values are passed for days and months, they
are rounded to the nearest integer. Weeks, quarters, and years are
converted to days or months, and then rounded to the nearest integer.




  moment().subtract(1.5, 'months') == moment().subtract(2, 'months')
moment().subtract(.7, 'years') == moment().subtract(8, 'months') //.7*12 = 8.4, rounded to 8

[#59438] Wednesday, January 4, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
skyler

Total Points: 646
Total Questions: 119
Total Answers: 96

Location: Bonaire
Member since Wed, Mar 29, 2023
1 Year ago
skyler questions
;