Javascript - Get Previous Months Date

后端 未结 6 900
情深已故
情深已故 2021-02-14 12:25

If i have a variable that returns a date, in the format of dd MMM yyyy, so 28 Aug 2014, how can i get the date of the previous month.

I can modify the month via:

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-14 13:11

    Just subtract the number of months from the month parameter and don't worry if the value is going to be negative. It will be handled correctly.

    new Date(2014, 0, 1) // 1st Jan 2014
    new Date(2014, -1, 1) // 1st Dec 2013
    

提交回复
热议问题