如何知道一个DateTime是DATERANGE之间在C# [英] How to know if a DateTime is between a DateRange in C#
本文介绍了如何知道一个DateTime是DATERANGE之间在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道,如果日期是DATERANGE之间。我有三个日期:
I need to know if a Date is between a DateRange. I have three dates:
// The date range
DateTime startDate;
DateTime endDate;
DateTime dateToCheck;
最简单的办法是做一个比较,但有一个更聪明的方式做到这一点?
The easy solution is doing a comparison, but is there a smarter way to do this?
在此先感谢。
推荐答案
不,做一个简单的比较,对我来说很好:
Nope, doing a simple comparison looks good to me:
return dateToCheck >= startDate && dateToCheck < endDate;
事情要考虑,虽然:
Things to think about though:
-
的DateTime
是一个有些奇怪的类型时区的条款。这可能是UTC,也可能是本地,也可能是不明确的。确保你比较苹果与苹果,因为它是。 - 在考虑你的起点和终点是否应包括或不包括。我做了code以上将其作为一个包容性的下界和上界。
DateTime
is a somewhat odd type in terms of time zones. It could be UTC, it could be "local", it could be ambiguous. Make sure you're comparing apples with apples, as it were.- Consider whether your start and end points should be inclusive or exclusive. I've made the code above treat it as an inclusive lower bound and an exclusive upper bound.
这篇关于如何知道一个DateTime是DATERANGE之间在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文