Format date and Subtract days using Moment.js

JavascriptDateMomentjs

Javascript Problem Overview


I would like a variable to hold yesterday's date in the format DD-MM-YYYY using Moment.js. So if today is 15-04-2015, I would like to subtract a day and have 14-4-2015.

I've tried a few combinations like this:

startdate = moment().format('DD-MM-YYYY');
startdate.subtract(1, 'd');

and this:

startdate = moment().format('DD-MM-YYYY').subtract(1, 'd');

and also this:

startdate = moment();
startdate.subtract(1, 'd');
startdate.format('DD-MM-YYYY')

But I'm not getting it...

Javascript Solutions


Solution 1 - Javascript

You have multiple oddities happening. The first has been edited in your post, but it had to do with the order that the methods were being called.

.format returns a string. String does not have a subtract method.

The second issue is that you are subtracting the day, but not actually saving that as a variable.

Your code, then, should look like:

var startdate = moment();
startdate = startdate.subtract(1, "days");
startdate = startdate.format("DD-MM-YYYY");

However, you can chain this together; this would look like:

var startdate = moment().subtract(1, "days").format("DD-MM-YYYY");

The difference is that we're setting startdate to the changes that you're doing on startdate, because moment is destructive.

Solution 2 - Javascript

var date = new Date();

var targetDate = moment(date).subtract(1, 'day').toDate(); // date object

Now, you can format how you wanna see this date or you can compare this date with another etc.

toDate() function is the point.

Solution 3 - Javascript

startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');

Solution 4 - Javascript

Try this:

var duration = moment.duration({'days' : 1});
moment().subtract(duration).format('DD-MM-YYYY');

This will give you 14-04-2015 - today is 15-04-2015

Alternatively if your momentjs version is less than 2.8.0, you can use:

startdate = moment().subtract('days', 1).format('DD-MM-YYYY');

Instead of this:

startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');

Solution 5 - Javascript

In angularjs moment="^1.3.0"

moment('15-01-1979', 'DD-MM-YYYY').subtract(1,'days').format(); //14-01-1979
or
moment('15-01-1979', 'DD-MM-YYYY').add(1,'days').format(); //16-01-1979
``


Solution 6 - Javascript

I think you have got it in that last attempt, you just need to grab the string.. in Chrome's console..

startdate = moment();
startdate.subtract(1, 'd');
startdate.format('DD-MM-YYYY');
"14-04-2015"

startdate = moment();
startdate.subtract(1, 'd');
myString = startdate.format('DD-MM-YYYY');
"14-04-2015"
myString
"14-04-2015"

Solution 7 - Javascript

startdate = moment().subtract(1, 'days').startOf('day')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbeaumondoView Question on Stackoverflow
Solution 1 - JavascriptnduggerView Answer on Stackoverflow
Solution 2 - JavascriptcanmustuView Answer on Stackoverflow
Solution 3 - JavascriptM. Adam KendallView Answer on Stackoverflow
Solution 4 - Javascriptuser1846747View Answer on Stackoverflow
Solution 5 - JavascriptRaphael VitorView Answer on Stackoverflow
Solution 6 - JavascriptlecstorView Answer on Stackoverflow
Solution 7 - JavascriptJethikView Answer on Stackoverflow