Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a date falls between a range of dates

I'm trying to learn javascript by writing my own events calendar and I ran into a challenge.

I'm doing this to check if a date falls between the start and end date of an event:

if(thisCellDate > startEventDate && thisCellDate < endEventDate) {}

thisCellDate is the current cell in a grid of days I'm looping over (the days in the current month). The problem is that when the start date is on the same day as the cell's in the grid it doesn't work because the date of the cell is technically earlier.

He is an example of what I'm talking about:

Start Date: Fri May 02 2014 15:01:16 GMT-0400 (Eastern Daylight Time)
Cell's Date: Sat May 02 2014 00:00:00 GMT-0400 (Eastern Daylight Time)
End Date: Thu May 29 2014 16:01:24 GMT-0400 (Eastern Daylight Time)

I am generation the Cell's date with a date string like: new Date('8,8,2014')

Now I was thinking I could solve this issue by just making the cell's date as close as possible to the next date like this: new Date(2013, 8, 8, 23, 59, 59, 999)

This way the event dates should always be earlier than the cell dates. Is there a problem doing it this way or is there a clearer way?

like image 419
red888 Avatar asked Jun 16 '14 14:06

red888


People also ask

How do you check if a date is within a date range?

To check if a date is between two dates: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.

Can you use Vlookup with date range?

The VLOOKUP function matches the largest date that is smaller or equal to the lookup date. If the lookup date is 3-31-2009 it will match 3-31-2009 found in cell B4 and return the corresponding value in column C (cell C4). In this case nothing, cell C4 is empty. This applies to all date ranges in column B.

How do I calculate date between two dates in Excel?

To find the number of days between these two dates, you can enter “=B2-B1” (without the quotes into cell B3). Once you hit enter, Excel will automatically calculate the number of days between the two dates entered. Note that Excel recognizes leap years.


1 Answers

When humans specify date-only ranges, they tend to be fully closed ranges. How many days are there from 2014-01-01 to 2014-01-02? TWO.

But when time is added to the mix, either in a time-only range, or a date+time range, then these ranges are usually half-open ranges. How many hours from 1:00 to 2:00? ONE.

Now the problem is that JavaScript's Date object is really a date+time object. (It's misnamed, IMHO). When you don't specify a time, it sets the time to the first moment of the day - which is usually (but not always) midnight.

So you might think that you would do this:

var a = new Date(2014, 0, 1);
var b = new Date(2014, 0, 2);
var inRange = dt >= a && dt <= b;

But that doesn't account for the time of day that's being secretly added by the Date object. A value in the middle of the day on Jan 2 would not be included in this range.

So to compensate, you have to add a day, then use a half-open interval:

var a = new Date(2014, 0, 1);
var b = new Date(2014, 0, 2);

var c = new Date(b.getTime());   // clone the date to not muck the original value
c.setDate(c.getDate()+1);        // add a day
var inRange = dt >= a && dt < c; // use a half-open interval

Using a shortened value like 23:59:59.999 might get you by, but it's not a good approach in general. Subtracting the start from the end of an interval should give you it's exact duration - not one millisecond less.

like image 108
Matt Johnson-Pint Avatar answered Sep 21 '22 12:09

Matt Johnson-Pint