If you have a moment which is a datetime.
var d1 = moment();
How do I convert d1
to a date
(losing time) so you can do math. I'd like to do..
var d1 = moment(); // convert this to a date.
var d2 = moment(); // convert this to a date.
These should return like this*;
d1.isBefore(d2) // false;
d1.isAfter(d2) // false;
d1.isSame(d2) // true
* assuming d2 didn't occur after the day-rollover
Try
moment().startOf('day')
That will shift the time to midnight (in most cases).
I'm not sure of the easiest way to do this in that fashion, but the comparison methods are smarter than that.. This is why the .valueOf
overrides weren't sufficient for the library.
d1.isBefore(d2); // same as d1 < d2;
But, the signature is
moment().isBefore(Moment|String|Number|Date|Array, String);
And, the second String
argument corresponds to this
Like moment#isAfter and moment#isSame, any of the units of time that are supported for moment#startOf are supported for moment#isBefore. Year, month, week, day, hour, minute, and second.
So simply run the same tests like this..
d1.isBefore(d2, 'day') // false;
d1.isAfter(d2, 'day') // false;
d1.isSame(d2, 'day') // true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.