Finding Last (or Next) Month
Problem
You need to calculate a relative date range like “last month” or “next month”.
Solution
Add or subtract from the current month, secure in the knowledge that JavaScript’s Date constructor will fix up the math.
Discussion
JavaScript Date objects will cheerfully handle underflows and overflows in the month and day fields, and will adjust the date object accordingly. You can ask for the 42nd of March, for example, and will get the 11th of April.
JavaScript Date objects store the year as the number of years since 1900, the month as an integer from 0 to 11, and the date (day of month) as an integer from 1 to 31. In the solution above, last_month_start is obtained by asking for the first day of a month in the current year, but the month is -1 to 10. If month is -1 the Date object will actually return December of the previous year:
The same is true for overflows: