Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date and Subtract days using Moment.js

People also ask

How do you subtract days using moments?

Use the subtract and format Methods We defined the startdate variable and assign the moment object with the date we want to subtract to it. Then we call subtract to subtract 1 day from startdate by calling it with 1 and 'days' . And then we call format to format the date with a formatting string.

How do you subtract days in JavaScript?

To subtract days to a JavaScript Date object, use the setDate() method. Under that, get the current days and subtract days. JavaScript date setDate() method sets the day of the month for a specified date according to local time.

How do you subtract two times using a moment?

MomentJS - Subtract Methodsubtract(2, 'months'); // subtract object method var changeddate1 = moment(). subtract({ days: 5, months: 2 }); //using duration in subract method var duration = moment. duration({ 'days': 10 }); var changeddate2 = moment([2017, 10, 15]). subtract(duration);

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().


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.


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

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.


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');

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"

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


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
``