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

后端 未结 2 1786
余生分开走
余生分开走 2021-01-04 17:46

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

Be

相关标签:
2条回答
  • 2021-01-04 18:17

    As of moment@2.15.1, 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);

    0 讨论(0)
  • 2021-01-04 18:34

    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>
    
    0 讨论(0)
提交回复
热议问题