如何知道一个DateTime是DATERANGE之间在C#

编程入门 行业动态 更新时间:2024-10-24 00:23:07
本文介绍了如何知道一个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#

本文发布于:2023-11-09 10:18:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DateTime   DATERANGE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!