日期范围(以天为单位)...C#

发布于 2024-10-07 18:54:02 字数 175 浏览 0 评论 0原文

我有一个从 C# 调用 Oracle DB 的查询。我想编写查询来获取最多 5 年的数据。

我目前有一个硬编码值 public const int FIVE_YEARS_IN_DAYS = 1825;

但是,由于闰年,这是不正确的。有没有一个函数可以给我过去 5 年的正确天数?

I have a query that is calling an Oracle DB from C#. I want to write the query to get data that is, at most, 5 years old.

I currently have a hard coded value for public const int FIVE_YEARS_IN_DAYS = 1825;

But, this isn't correct because of leap years. Is there a function that will give me the correct number of days in the preceeding 5 years?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

峩卟喜欢 2024-10-14 18:54:02

我想你想要这个:

DateTime now = DateTime.Now;    
now.AddYears(-5).Subtract( now ).Days

I think you want this:

DateTime now = DateTime.Now;    
now.AddYears(-5).Subtract( now ).Days
优雅的叶子 2024-10-14 18:54:02
DateTime now = DateTime.Now;
TimeSpan fiveYears = now.Subtract(now.AddYears(-5));
int numberOfDaysInLastFiveYears = fiveYears.Days;

这将正确解释闰年。现在执行此操作可获得 1,826 天。

DateTime now = DateTime.Now;
TimeSpan fiveYears = now.Subtract(now.AddYears(-5));
int numberOfDaysInLastFiveYears = fiveYears.Days;

This will correctly account for leap years. Doing this right now yields 1,826 days.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文