Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MomentJS Day diff dates only excluding time

Is there a way of comparing 2 dates without time with momentJS?

I've tried various formats, but can't seem to get it to work.

I have it working fine with dateTime:

var ExpiryDate = new Date("11/13/2014 11:13:00");
var daysDiff = moment(ExpiryDate).diff(moment(Date.now()), 'days');

I'm looking to get the result to be 14 days no matter what time of day.

Thanks

like image 554
AJTECH Avatar asked Nov 03 '14 11:11

AJTECH


People also ask

How do you calculate the difference between two dates in a moment?

Date difference with Moment objects You can also create Moment objects and then calculate the difference: var admission = moment('{date1}', 'DD-MM-YYYY'); var discharge = moment('{date2}', 'DD-MM-YYYY'); discharge. diff(admission, 'days');

How do I remove time from moment date?

We can remove the time portion from a date with the startOf method. We pass in a date with the hour and minutes set into the moment function. Then we call startOf with the 'day' argument to get return a moment object with the time set to midnight of the same date.

How can I check whether the time is between two times using moment JS?

js isBetween() Function. It is used to check whether a date is between a range in Moment. js using the isBetween() function that checks if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc).


1 Answers

You can use the startOf function to remove the time components of both dates

moment(ExpiryDate).startOf('day').diff(moment(Date.now()).startOf('day'), 'days');
like image 79
codebox Avatar answered Oct 04 '22 13:10

codebox