Is Between - momentjs.com. Check if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc). The match is exclusive. The first two arguments will be parsed as moments, if not already so.
To check if a date is between two dates:Use the Date() constructor to convert the dates to Date objects. Check if the date is greater than the start date and less than the end date. If both conditions are met, the date is between the two dates.
To get a date range using moment. js and JavaScript, we can use the moment add method. to set the start and end date of the range to startDate and endDate . Then we set d to startDate .
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
You can use one of the moment plugin -> moment-range to deal with date range:
var startDate = new Date(2013, 1, 12)
, endDate = new Date(2013, 1, 15)
, date = new Date(2013, 2, 15)
, range = moment().range(startDate, endDate);
range.contains(date); // false
You can use
moment().isSameOrBefore(Moment|String|Number|Date|Array);
moment().isSameOrAfter(Moment|String|Number|Date|Array);
or
moment().isBetween(moment-like, moment-like);
See here : http://momentjs.com/docs/#/query/
I do believe that
if (startDate <= date && date <= endDate) {
alert("Yay");
} else {
alert("Nay! :(");
}
works too...
Good news everyone, there's an isBetween
function!
Update your library ;)
http://momentjs.com/docs/#/query/is-between/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With