Moment.js: Date between dates
Asked Answered
B

9

164

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 comparison.

Since there's no isBetween() method, I thought this would work:

var date = 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");

if (date.isBefore(endDate) && date.isAfter(startDate) || (date.isSame(startDate) || date.isSame(endDate)) ) { alert("Yay!"); } else { alert("Nay! :("); }

I'm convinced there's got to be a better way to do this. Any ideas?

Bacillus answered 15/2, 2013 at 15:9 Comment(2)
I'm confident you wanted isBetween || isStart || isEnd?Lucknow
Yeah, typo there, sorry!Bacillus
R
101

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
Repulse answered 15/2, 2013 at 15:56 Comment(7)
how do you include the plugin?Gujranwala
You should just include <script src="/javascripts/moment-range.js"></script> after momentjsRepulse
date.isBetween(startDate, endDate); is much simpler and avoids requiring an additional plugin.Abessive
moment & moment-Range provides options to format date time while preparing ranges and then compare the dates for Contains / Within / Overlaps / Intersect / Add / Subtract. if some one retrieving dates in some special format, then one can able to prepare date ranges inside array by converting those dates with required format and then we can compare.Lyndel
For React after npm install moment, import moment from 'moment';Preeminent
Is creating a range going to be more resource intensive than the answer below, using moment.isBetween ?Preeminent
this will be helpful ``` const Moment = require('moment'); const MomentRange = require('moment-range'); const moment = MomentRange.extendMoment(Moment); ```Rolle
G
374

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

Gipson answered 7/4, 2015 at 15:45 Comment(4)
x.isBetween(moment(a).subtract(1, 'day'), b) seems to do the trick as well.Roquefort
@Gipson That's a good workaround; might I suggest another one with fewer function calls: !(x.isBefore(a) || x.isAfter(b))Saltandpepper
What does exclusive mean?Alt
@Alt Consider the list 1,2,3,4,5. Exclusively, 1 and 5 are not between the values of this list. Inclusively, 1 and 5 are between the values of this list. Left inclusive (same as right exclusive) includes 1, but not 5. Right inclusive (same as left exclusive) includes 5, but not 1.Gipson
R
101

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
Repulse answered 15/2, 2013 at 15:56 Comment(7)
how do you include the plugin?Gujranwala
You should just include <script src="/javascripts/moment-range.js"></script> after momentjsRepulse
date.isBetween(startDate, endDate); is much simpler and avoids requiring an additional plugin.Abessive
moment & moment-Range provides options to format date time while preparing ranges and then compare the dates for Contains / Within / Overlaps / Intersect / Add / Subtract. if some one retrieving dates in some special format, then one can able to prepare date ranges inside array by converting those dates with required format and then we can compare.Lyndel
For React after npm install moment, import moment from 'moment';Preeminent
Is creating a range going to be more resource intensive than the answer below, using moment.isBetween ?Preeminent
this will be helpful ``` const Moment = require('moment'); const MomentRange = require('moment-range'); const moment = MomentRange.extendMoment(Moment); ```Rolle
D
34

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/

Dent answered 21/10, 2016 at 10:55 Comment(1)
Quick note: isBetween requires moment 2.13.0. momentjs.com/docs/#/query/is-betweenOry
P
16

I do believe that

if (startDate <= date && date <= endDate) {
  alert("Yay");
} else {
  alert("Nay! :("); 
}

works too...

Polka answered 10/4, 2014 at 11:55 Comment(2)
This calculation always ignores the year. For example, If I give startDate ='05-01-2019', endDate = '05-31-2019' and date as '05-21-2017', it will give me result as "Yay", although it is false.Teddie
@AakashMaurya You are comparing strings, not dates. Make sure that you define your startDate/endDate as startDate=new Date("05-01-2019").Polka
W
15

Good news everyone, there's an isBetween function! Update your library ;)

http://momentjs.com/docs/#/query/is-between/

Whippletree answered 17/3, 2015 at 19:10 Comment(2)
True but isBetween isn't inclusiveDwarfism
Version 2.13.0 introduces exclusivity. A [ indicates inclusion of a value. A ( indicates exclusion. If the inclusivity parameter is used, both indicators must be passed. moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '[]'); //trueConsideration
O
14

Please use the 4th parameter of moment.isBetween function (inclusivity). Example:

var startDate = moment("15/02/2013", "DD/MM/YYYY");
var endDate = moment("20/02/2013", "DD/MM/YYYY");

var testDate = moment("15/02/2013", "DD/MM/YYYY");

testDate.isBetween(startDate, endDate, 'days', true); // will return true
testDate.isBetween(startDate, endDate, 'days', false); // will return false
Oshaughnessy answered 17/10, 2016 at 12:28 Comment(2)
any documentation link for this?Silurian
they introduced a small change in the 4th parameter on the new version, but it still works as the inclusivity parameter. here's the documentation momentjs.com/docs/…Oshaughnessy
F
7
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.

Fleischman answered 5/3, 2015 at 10:26 Comment(0)
N
1

As Per documentation of moment js,

There is Precise Range plugin, written by Rob Dawson, can be used to display exact, human-readable representations of date/time ranges, url :http://codebox.org.uk/pages/moment-date-range-plugin

moment("2014-01-01 12:00:00").preciseDiff("2015-03-04 16:05:06");
// 1 year 2 months 3 days 4 hours 5 minutes 6 seconds

moment.preciseDiff("2014-01-01 12:00:00", "2014-04-20 12:00:00");
// 3 months 19 days
Nebulize answered 12/11, 2014 at 12:1 Comment(0)
T
0

According to momentjs docs for inclusion and exclusion we use 4th parameter which takes in [] or () or combination of both, bascially "[" means include and "(" means exclude so bascially if we use left "[" or "(" this means date from will be included and if we use right "]" or ")" this means date to will be included

moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', undefined, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', undefined, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', undefined, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', undefined, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', undefined, '[]'); //true

So to include both we use "[]" in 4rth parameter.

Momentjs Offical Documentation for isBetween

enter image description here

Topside answered 31/1, 2024 at 12:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.