Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract one month using moment.js? [duplicate]

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?

like image 789
Bhanuchandar Challa Avatar asked Jan 06 '17 12:01

Bhanuchandar Challa


People also ask

How do you subtract one day in a moment?

var startdate = moment(). subtract(1, "days"). format("DD-MM-YYYY");

How do you find the month from a moment?

The moment(). daysInMonth() function is used to get the number of days in month of a particular month in Node.


1 Answers

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 
like image 151
Dipiks Avatar answered Sep 24 '22 13:09

Dipiks