Javascript - Get Previous Months Date

var myVariable = "28 Aug 2014"
var makeDate = new Date(myVariable);
makeDate = new Date(makeDate.setMonth(makeDate.getMonth() - 1));

Update:

A shorter version:

var myVariable = "28 Aug 2014"
var makeDate = new Date(myVariable);

console.log('Original date: ', makeDate.toString());

makeDate.setMonth(makeDate.getMonth() - 1);

console.log('After subtracting a month: ', makeDate.toString());

Update 2:

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


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