如何知道DateTime是否在C#中的DateRange之间 [英] How to know if a DateTime is between a DateRange in C#
本文介绍了如何知道DateTime是否在C#中的DateRange之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道日期是否介于 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;
需要考虑的事情:
DateTime
在时区方面有点奇怪.它可能是UTC,可能是本地",也可能是模棱两可的.确保您将苹果与苹果进行比较.- 考虑您的起点和终点应该是包容性的还是排斥性的.我已经让上面的代码把它当作一个包含下限和一个排他上限.
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是否在C#中的DateRange之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文