javascript 检查日期是否介于日期范围之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24246092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a date falls between a range of dates
提问by red888
I'm trying to learn javascript by writing my own events calendar and I ran into a challenge.
我正在尝试通过编写自己的活动日历来学习 javascript,但遇到了挑战。
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.
thisCellDate 是我循环的天数网格中的当前单元格(当前月份的天数)。问题是,当开始日期与网格中的单元格在同一天时,它不起作用,因为单元格的日期在技术上更早。
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')
我正在使用日期字符串生成 Cell 的日期,例如: 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)
现在我想我可以通过像这样使单元格的日期尽可能接近下一个日期来解决这个问题: 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?
这样事件日期应该总是早于单元格日期。这样做有问题还是有更清晰的方法?
回答by Matt Johnson-Pint
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.
当人类指定仅限日期的范围时,它们往往是完全封闭的范围。从2014-01-01
到有多少天2014-01-02
?二。
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.
但是,当时间被添加到混合中时,无论是在仅时间范围内,还是在日期+时间范围内,这些范围通常是半开放范围。从1:00
到多少小时2:00
?一。
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.
现在的问题是 JavaScript 的Date
对象实际上是一个日期+时间对象。(它的名字有误,恕我直言)。当您不指定时间时,它会将时间设置为一天中的第一个时刻 - 通常(但不总是)午夜。
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.
但这并没有说明Date
对象秘密添加的时间。1 月 2 日中午的值将不包括在此范围内。
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.
使用像 23:59:59.999 这样的缩短值可能会让您满意,但这通常不是一个好方法。从间隔的末尾减去开始应该给你确切的持续时间 - 而不是少一毫秒。