Date range in days .... C #

I have a query that calls Oracle DB with C #. I want to write a request to get data, maximum 5 years.

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

But this is not true due to leap years. Is there a function that will give me the correct number of days in the previous 5 years?

+4
source share
2 answers

I think you want this:

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

This will correctly take into account leap years. The exercise of this right now gives 1826 days.

+4
source

Source: https://habr.com/ru/post/1332261/


All Articles