Skip to content
Advertisement

Javascript – Get Previous Months Date

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:

JavaScript

Essentially, this is adding one to the Month.. But how can i account for years, so if the current date is 12 Dec 2014, the previous would be 12 Jan 2013?

My application is using AngularJS can make use of filters.

UPDATE:

JavaScript

How comes although the month has incremented, the day is showing as 01 instead of 28?

Advertisement

Answer

JavaScript

Update:

A shorter version:

JavaScript

Update 2:

If you don’t want to deal with corner cases just use moment.js. Native JavaScript API for Date is bad.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement