The
Solution 1
given by
JF2015 is good when the period between
tm1 and tm2
is greater than
one month
. But when the period is less than one month say
tm1=15-Jan-2012
and
tm2=08-Feb-2012
, it returns
Months: 1 Days: -7
.
I think the following code can be used for both when the
period is more than one month and when the period is less than a month
.
DateTime date1 = DateTime.ParseExact("15-Jan-2012","d-MMM-yyyy",
System.Globalization.CultureInfo.InvariantCulture);
DateTime date2 = DateTime.ParseExact("08-Feb-2012","d-MMM-yyyy",
System.Globalization.CultureInfo.InvariantCulture);
while(date1.AddMonths(1) < date2){
date1 = date1.AddMonths(1);
}
int days = date2.Subtract(date1).Days;
Console.WriteLine (days);