Difference in months between two dates
Asked 07 September, 2021
Viewed 520 times
  • 66
Votes

How to calculate the difference in months between two dates in C#?

Is there is equivalent of VB's DateDiff() method in C#. I need to find difference in months between two dates that are years apart. The documentation says that I can use TimeSpan like:

TimeSpan ts = date1 - date2;

but this gives me data in Days. I don't want to divide this number by 30 because not every month is 30 days and since the two operand values are quite apart from each other, I am afraid dividing by 30 might give me a wrong value.

Any suggestions?

30 Answer