Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  77] [ 6]  / answers: 1 / hits: 26243  / 10 Years ago, mon, june 16, 2014, 12:00:00

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?


More From » datetime

 Answers
11

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.


[#70551] Friday, June 13, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;