Moment.js: Date between dates

后端 未结 8 1510
庸人自扰
庸人自扰 2020-11-27 11:18

I\'m trying to detect with Moment.js if a given date is between two dates. Since version 2.0.0, Tim added isBefore() and isAfter() for date compari

8条回答
  •  無奈伤痛
    2020-11-27 11:58

    if (date.isBefore(endDate) 
     && date.isAfter(startDate) 
     || (date.isSame(startDate) || date.isSame(endDate))
    

    is logically the same as

    if (!(date.isBefore(startDate) || date.isAfter(endDate)))
    

    which saves you a couple of lines of code and (in some cases) method calls.

    Might be easier than pulling in a whole plugin if you only want to do this once or twice.

提交回复
热议问题