javascript moment.js 日期比较 - 今天的前一天还是今天的后两天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27422381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
moment.js date comparison - day before today or two days after today?
提问by Terje Nyg?rd
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".
我正在努力使用 moment.js“查询”来确定日期(例如:12/10/2014)是否在“今天”前一天或“今天”后两天的范围内。
Been googling around, and checking the moment.js documentation, but haven't found any proper or understandable examples on how to do this...
一直在谷歌搜索,并检查 moment.js 文档,但没有找到任何关于如何执行此操作的正确或可理解的示例......
回答by Matt Roman
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>
回答by diosney
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.
截至[email protected]
,有一种isBetween
方法可以检查日期是否在两个日期之间,具有包容性和独占性支持。
Check http://momentjs.com/docs/#/query/is-between/
检查http://momentjs.com/docs/#/query/is-between/
Example:
例子:
moment(dateToCheck).isBetween(startDate, endDate);
moment(dateToCheck).isBetween(startDate, endDate);