In versions 2.9+ there is an isBetween
function, but it's exclusive:
var compareDate = moment("15/02/2013", "DD/MM/YYYY");
var startDate = moment("12/01/2013", "DD/MM/YYYY");
var endDate = moment("15/01/2013", "DD/MM/YYYY");
// omitting the optional third parameter, 'units'
compareDate.isBetween(startDate, endDate); //false in this case
There is an inclusive workaround ...
x.isBetween(a, b) || x.isSame(a) || x.isSame(b)
... which is logically equivalent to
!(x.isBefore(a) || x.isAfter(b))
In version 2.13 the isBetween
function has a fourth optional parameter, inclusivity
.
Use it like this:
target.isBetween(start, finish, 'days', '()') // default exclusive
target.isBetween(start, finish, 'days', '(]') // right inclusive
target.isBetween(start, finish, 'days', '[)') // left inclusive
target.isBetween(start, finish, 'days', '[]') // all inclusive
More units to consider: years, months, days, hours, minutes, seconds, milliseconds
Note: units are still optional. Use null
as the third argument to disregard units in which case milliseconds is the default granularity.
Visit the Official Docs
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…