Terje Nygård
Terje Nygård

Reputation: 1257

moment.js date comparison - day before today or two days after today?

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

Upvotes: 14

Views: 31292

Answers (2)

Diosney
Diosney

Reputation: 10580

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

Upvotes: 5

Matt Roman
Matt Roman

Reputation: 436

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>

Upvotes: 33

Related Questions