Javascript – Get Previous Months Date

All we need is an easy explanation of the problem, so here it is.

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?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 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.

Method 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

Method 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.

Method 4

You could use moment.js subtract function for this.

var day = moment("2014-08-28");
day.subtract(1, 'months');

Method 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());

Method 6

There is my way to get previous months.

const formatter = new Intl.DateTimeFormat("default", {
  month: "long"
});
const date = new Date();

let number = 1;
let prevMonth = formatter.format(
  new Date(date.getFullYear(), date.getMonth() - `${number}`)
);

Method 7

This returns all previous months with the beginning and end of the month

     var now = new Date();
            var start_month = new Date();
            start_month.setMonth(now.getMonth() - 5);
            for (var d = start_month; d <= now; d.setMonth(d.getMonth() + 1)) {
                var firstDay = new Date(d.getFullYear(), d.getMonth(), 1);
                var x = new Date(firstDay);
                x.setDate(0);
                x.setDate(1);

                var date = new Date(x);
                var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
                var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

                var startDate = `${firstDay.getFullYear()}-${firstDay.getMonth() + 1}-${firstDay.getDate()}`;
                var endDate = `${lastDay.getFullYear()}-${lastDay.getMonth() + 1}-${lastDay.getDate()}`;
                console.log(startDate)
                console.log(endDate)
            }

Method 8

var makeDate = new Date();

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

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

console.log('After subtracting half year: ', makeDate.toString());

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply