Get exact days and months between two dates c#

80 Views Asked by At

I have two dates viz startDate and endDate. I want to calculate the exact number of days and months between these dates including the endDate. The scenario is I am disabling the dates prior to today and dates after 6 months from the startDate. It works fine for the most part. But if I select startDateas and endDate i.e 10/06/2015-08/12/2015 the total number of days and months should be 5 Months and 29 days. But I get the dates as 6 months and 2 days.

Here is my code:-

int totalDays = (endDate - startDate).Days;
double totalMonths = Math.Truncate(((double)totalDays % 365) / 30);
double remainingDays = Math.Truncate(((double)totalDays % 365) % 30);

And

            DateTime date = new DateTime(new TimeSpan(totalDays, 0, 0, 0).Ticks);
         int  totalMonths = date.Month - 1;
          int  remainingDays = date.Day-1;

Both the above snippets seem to give the same results. How can I solve this issue? Any suggesstions would be appreciated.

0

There are 0 best solutions below