How to calculate the number of months between two dates in C#
c#-4.0
Solution
Assuming you want the number of whole months, I think this should work (not sure what corner-cases this doesn't handle, but can't think of any off the top of my head):
12 * (date1.Year - DateOfDeposit.Year) + (date1.Month - DateOfDeposit.Month)
You could always add any fractional component with DateTime.Day, but it's not clear how that should precisely be defined (how many months are between Jan 29 and Feb 27?).
If you're doing a lot of date-time handling, Jon Skeet's Noda Time library (that he mentions in his answer) is a good choice.
Problem
I am doing this ``` datediff = (date1 - DateOfDeposit).TotalDays; ``` But this gives the no. of days and I want no.of months.