Javascript - Get Previous Months Date
Solution 1
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.
Solution 2
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
Solution 3
For all date related activities I recommend using the moment.js library and as you are using AngularJS there is a library for this: https://github.com/urish/angular-moment.
Take a look at the moment.js documentation and you will see it makes it much easier to manipulate dates in JavaScript: http://momentjs.com/docs/#/get-set/month/
You will easily be able to solve the problem you are facing, for example:
var now = moment('01/01/2014').subtract(1, 'months').format('DD/MM/YYYY');
now would then equal 01/12/2013.
Solution 4
You could use moment.js subtract function for this.
var day = moment("2014-08-28");
day.subtract(1, 'months');
Solution 5
var myVariable = "28 Aug 2014"
var makeDate = new Date(myVariable);
console.log('Original date: ', makeDate.toString());
makeDate.setMonth(makeDate.getMonth() - 6);
console.log('After subtracting half year: ', makeDate.toString());
Oam Psy
Updated on September 22, 2021Comments
-
Oam Psy over 2 years
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:
$scope.DateMapping = function (months) { var myVariable = "28 Aug 2014" var makeDate = new Date(myVariable); prev = new Date(makeDate.getFullYear(), makeDate.getMonth()+1, 1); });
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:
var myVariable = "28 Aug 2014" var makeDate = new Date(myVariable); var prev = new Date(makeDate.getFullYear(), makeDate.getMonth()+1, makeDate.getMonth()); console.log(myVariable) console.log(makeDate) console.log(prev) Output: 28 Aug 2014 Thu Aug 28 2014 00:00:00 GMT+0100 (GMT Standard Time) Mon Sep 01 2014 00:00:00 GMT+0100 (GMT Standard Time)
How comes although the month has incremented, the day is showing as 01 instead of 28?
-
Oam Psy over 9 yearsThanks, ideally i would like to achieve this without another library.. Please see my updated. I expected Angular to do something like this very easily.
-
Oam Psy over 9 years@Dimitry - can i minus the day by 1 too? e.g. makeDate = new Date(makeDate.setMonth(makeDate.getMonth() - 1), makeDate.setDate(makeDate.getDate() - 1));
-
Dmitry over 9 years
makeDate = new Date(new Date(makeDate.setMonth(makeDate.getMonth() - 1)).setDate(makeDate.getDate() - 1));
-
Asif Mehmood over 7 yearsDoes it work if i say i have date "31 Aug 2014" and i want next months date. Wouldn't this logic give error if i add one month because September does not have 31st day (just asking, if that happens). I guess it would take me to 1st, October, instead of some date that is present in September.
-
sabithpocker over 6 yearsMoment is no longer recommended until its ready for treeshaking. date-fns is recommended for Angular5. (my personal recommendation)
-
smac89 over 6 yearsI hope people are reading the above code correctly, the second one does not mean "subtract one month from the current date", it actually means go to month 0 (Jan) and subtract one month, thus going back to December of the previous year. Pay careful attention to the commented part beside it
-
Serkan KONAKCI about 5 yearsThis method will not work for "Fri Mar 29 2019" // -> will return Fri Mar 01 2019
-
Dmitry about 4 years@CharinduEdirisooriya yep, does not looks like there is a simple way to do it without moment.js
-
Elinoter99 about 4 years@Dmitry newDate.setDate( date.getDate() - 31 ); console.log(newDate); return newDate;
-
Dmitry about 4 years@CharinduEdirisooriya this is kind of works, but if you use it on say '28 Mar 2014' you'll get '25 Feb 2014'. However in moment.js it works as intended.