检查日期时间实例是否位于其他两个日期时间对象之间
我想知道一个简单的算法来检查给定的 datetime
实例是否位于 C#
中的另外两个实例之间。
注意:
我浏览了一下如何检查给定的日期时间对象是否在两个日期时间“之间”? 它适用于 python,还有更多适用于 php。大多数其他问题都是关于两者之间的差异。
详细信息:
我对时间更具体,日期对我来说并不重要。例如,我获得了在 10:00
Am - 9:00
Pm 之间工作的员工的 DataBase
条目,我想知道是哪位员工在指定时间上课,例如 2:00 Pm
。现在,这将返回我此时正在参与的员工的详细信息。
编辑
接受答案后(一年多前),我意识到我错误地描述了问题。但我认为当时要做的就是进行日期和时间比较。所以 Jason 和 VikciaR 的回答都是有效的。
I would like to know a simple algorithm to check if the given instance of datetime
lies between another two instances in C#
.
Note:
I skimmed though this How do I check if a given datetime object is "between" two datetimes? and it was for python and many more for php. Most of the other questions were regarding difference between the two.
Details:
I am more specific about the time, date does not matter to me. For example i got DataBase
entry for a staff who works between 10:00
Am - 9:00
Pm and I would like to know which staff is engaged in class at the given time like 2:00 Pm
. Now this would return me the staff's details who are engaged at this time.
Edit
After accepting the answer(been more than year back), i realized i had incorrectly described the problem. But all i think that was to be done back then was to do date and time comparison. So answers by both Jason and VikciaR work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DateTime.Ticks 将考虑时间。在 DateTime 上使用 .Ticks 将日期转换为长整型。然后只需使用一个简单的 if stmt 来查看您的目标日期是否介于两者之间。
DateTime.Ticks will account for the time. Use .Ticks on the DateTime to convert your dates into longs. Then just use a simple if stmt to see if your target date falls between.
做简单比较>和<。
如果您只关心时间:
Do simple compare > and <.
If you care only on time:
自己编写一个辅助函数:
然后调用:
.IsBewteenTwoDates(DateTime.Today ,new DateTime(,,));
Write yourself a Helper function:
Then call:
.IsBewteenTwoDates(DateTime.Today ,new DateTime(,,));
您可以使用:
You can, use:
您可以使用:
或
You can use:
or