Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate difference between two dates (number of days)?

Tags:

date

c#

I see that this question has been answered for Java, JavaScript, and PHP, but not C#. So, how might one calculate the number of days between two dates in C#?

like image 564
leora Avatar asked Oct 22 '09 13:10

leora


People also ask

How do you calculate the number of days between two dates?

To calculate the number of days between two dates, you need to subtract the start date from the end date.

How do I calculate the difference between two dates and days in Excel?

To find the number of days between these two dates, you can enter “=B2-B1” (without the quotes into cell B3). Once you hit enter, Excel will automatically calculate the number of days between the two dates entered.


2 Answers

The top answer is correct, however if you would like only WHOLE days as an int and are happy to forgo the time component of the date then consider:

(EndDate.Date - StartDate.Date).Days 

Again assuming StartDate and EndDate are of type DateTime.

like image 22
Darren Avatar answered Oct 03 '22 00:10

Darren


Assuming StartDate and EndDate are of type DateTime:

(EndDate - StartDate).TotalDays 
like image 132
Greg Beech Avatar answered Oct 02 '22 23:10

Greg Beech