Moment.js: date between dates

I am trying to detect with Moment.js if the given date is between two dates. Starting with version 2.0.0, Tim added isBefore() and isAfter() to compare dates.

Since there is 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 am convinced that this is the best way to do this. Any ideas?

+76
javascript date momentjs
Feb 15 '13 at 15:09
source share
8 answers

You can use one of the moment plugins -> moment-range to deal with a 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 
+58
Feb 15 '13 at 15:56
source share

In versions 2.9 + there is an isBetween function, but it is 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 a workaround ...
x.isBetween(a, b) || x.isSame(a) || x.isSame(b)

... which is logically equivalent !(x.isBefore(a) || x.isAfter(b))


In version 2.13 , the isBetween function has a fourth optional parameter, inclusivity .

Use it as follows:

 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 ignore units, in which case milliseconds are the default granularity.

Visit official docs

+173
Apr 7 '15 at 15:45
source share

I really think that

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

works too ...

+14
Apr 10 '14 at
source share

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/

+14
Oct 21 '16 at 10:55
source share

The good news is there isBetween ! Update your library;)

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

+11
Mar 17 '15 at 19:10
source share

Please use the 4th parameter of the moment.isBetween function (inclusion). 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 
+5
Oct 17 '16 at 12:28
source share
 if (date.isBefore(endDate) && date.isAfter(startDate) || (date.isSame(startDate) || date.isSame(endDate)) 

logically coincides with

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

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

It may be easier than pulling the whole plugin if you want to do it once or twice.

+4
Mar 05 '15 at 10:26
source share

As in js moment documentation,

There is a Precise Range plugin written by Rob Dawson that can be used to display accurate, 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 
+1
Nov 12 '14 at 12:01
source share



All Articles