检查日期时间实例中的其他两个日期时间对象之间的落在 [英] Check if datetime instance falls in between other two datetime objects

查看:115
本文介绍了检查日期时间实例中的其他两个日期时间对象之间的落在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个简单的算法来检查日期时间的给定实例在 C#

I would like to know a simple algorithm to check if the given instance of datetime lies between another two instances in C#.

注意:

我脱脂尽管这<一个href=\"http://stackoverflow.com/questions/2634127/how-do-i-check-if-a-given-datetime-object-is-between-two-datetimes\">How我检查,如果给定的日期时间对象&QUOT ...之间QUOT; 2日期时间?的,这是蟒蛇,多为PHP。大多数其他问题是关于两者之间的区别。

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.

详细内容:

我具体说一下时间,日期并不重要,我。例如我得到数据库项的工作人员谁 10:00 AM之间工程 - 9 :00 PM和我作为校长[例如]想知道哪些员工在给定的时间是从事像类下午2:00 。现在,这将返回我的工作人员的详细信息是谁在这个时候搞。

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 me as principal[for example] 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.

推荐答案

DateTime.Ticks将占时间。使用的日期时间.Ticks你的日期转换为多头。然后,只需使用一个简单的,如果看到语句,如果你的目标日期间下降。

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.

// Assuming you know d2 > d1
if (targetDt.Ticks > d1.Ticks && targetDt.Ticks < d2.Ticks)
{
    // targetDt is in between d1 and d2
}  

这篇关于检查日期时间实例中的其他两个日期时间对象之间的落在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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