检查日期是否在日期范围之间 [英] Checking if a date falls between a range of dates

查看:110
本文介绍了检查日期是否在日期范围之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过编写自己的事件日历来学习JavaScript,但遇到了挑战。

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是我要遍历的天数网格中的当前单元格(当前月)。问题在于,当开始日期与该单元格在网格中的同一天时,它将无法正常工作,因为该单元格的日期在技术上更早。

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.

是我在说什么的示例:

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)

我正在生成带有日期字符串的单元格日期,例如: new Date('8,8,2014')

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

现在我想我可以通过使单元格的日期尽可能接近来解决此问题尽可能这样直到下一个日期: new Date(2013,8,8,23,59,59,999)

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?

推荐答案

当人类指定仅日期范围时,他们倾向于完全封闭范围。从 2014-01-01 2014-01-02 有多少天?两个。

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.

但是,当将时间添加到混合中时,无论是在仅时间范围内还是在日期+时间范围内,那么这些范围通常都是半开放范围。从 1:00 2:00 多少小时?一个。

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.

现在的问题是JavaScript的 Date 对象实际上是一个日期+时间对象。 (名字错误,恕我直言)。如果您未指定时间,则将时间设置为一天的第一时刻-通常(但并非总是)午夜。

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;

但这并不说明<$ c $秘密添加的时间c> Date 对象。

因此,为补偿起见,您必须添加一天,然后使用一半开放时间间隔:

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

使用诸如23:59:59.999之类的缩短值可能会让您,但这通常不是一个好方法。从间隔的结尾减去开始应该可以为您提供确切的持续时间-少一毫秒。

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.

这篇关于检查日期是否在日期范围之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆