开发者

Date Range in days....C#

开发者 https://www.devze.com 2023-01-30 14:17 出处:网络
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 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 preceedin开发者_如何转开发g 5 years?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消