[c#] Calculate difference between two dates (number of days)?

There often is a debate on time (hours) when it comes to counting days between two dates. The responses to the question and their comments show no exception.

If performance is not a concern, I would strongly recommend documenting your calculation through intermediate conversions. For example, (EndDate - StartDate).Days is unintuitive because rounding will depend on the hour component of StartDate and EndDate.

  • If you want the duration in days to include fractions of days, then as already suggested use (EndDate - StartDate).TotalDays.
  • If you want the duration to reflect the distance between two days, then use (EndDate.Date - StartDate.Date).Days
  • If you want the duration to reflect the duration between the morning of the start date, and the evening of the end date (what you typically see in project management software), then use (EndDate.Date - StartDate.Date).Days + 1