Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

$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?

like image 985
Oam Psy Avatar asked Oct 21 '14 13:10

Oam Psy


People also ask

How to get previous month first date in JavaScript?

Use the Date() constructor to get the first and last day of the previous month. The first 3 parameters the Date() constructor takes are the year, month and day of the month. The constructor returns a new Date object according to the supplied parameters.

How can I get previous month in moment?

An even easier solution would be to use moment. date(0) . the . date() function takes the 1 to n day of the current month, however, passing a zero or negative number will yield a dates in the previous month.

How do I calculate the date in JavaScript three months prior to today?

First select the date object. Then use the getMonth() method to get the months. Then subtract three months from the getMonth() method and return the date.

How do I get previous month in react?

To get the previous month name, first we need to access the current date object using the new Date() constructor. const current = new Date(); Now, we need to subtract the current month with -1 and set it to the current date using the setMonth() and getMonth() methods.


3 Answers

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.

like image 180
Dmitry Avatar answered Oct 20 '22 03:10

Dmitry


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
like image 9
RaYell Avatar answered Oct 20 '22 02:10

RaYell


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.

like image 5
Leon Revill Avatar answered Oct 20 '22 02:10

Leon Revill