I'm struggling with a moment.js "query" to figure out if a date (ex : 12/10/2014) is within the range of the day before "today", or two days after "today".
Been googling around, and checking the moment.js documentation, but haven't found any proper or understandable examples on how to do this...
We can use the isAfter method to check if one date is after another. We create a moment object with a date string. Then we call isAfter on it with another date string and the unit to compare.
However, you can chain this together; this would look like: var startdate = moment(). subtract(1, "days"). format("DD-MM-YYYY");
to create the tomorrow variable that's set to a moment object with today's date. Then we call add with 1 and 'days' to add one day to today. And then we call format with 'YYYY-MM-DD' to format the date to YYYY-MM-DD format.
Using moment, you can do the following...
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
var now = moment(),
begin = moment().subtract(1, 'days').startOf('day'),
end = moment().add(2, 'days').endOf('day')
document.write(now.isAfter(begin) && now.isBefore(end))
</script>
As of [email protected]
, there is a isBetween
method that allow to check if a date is between two dates, with inclusive and exclusive support.
Check http://momentjs.com/docs/#/query/is-between/
Example:
moment(dateToCheck).isBetween(startDate, endDate);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With