简体   繁体   English

日期范围(天)...... C#

[英]Date Range in days…C#

I have a query that is calling an Oracle DB from C#. 我有一个从C#调用Oracle DB的查询。 I want to write the query to get data that is, at most, 5 years old. 我想编写查询以获取最多5年的数据。

I currently have a hard coded value for public const int FIVE_YEARS_IN_DAYS = 1825; 我目前有一个public const int FIVE_YEARS_IN_DAYS = 1825;的硬编码值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? 是否有一项功能可以在前5年内给我正确的天数?

I think you want this: 我想你想要这个:

DateTime now = DateTime.Now;    
now.AddYears(-5).Subtract( now ).Days
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. 现在这样做可以产生1,826天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM