14,455,338 members
Rate this:
See more:
How to Calculate difference between two dates in year, month and day

Eg: 01/08/2012 - 31/08/2012 then i should get result as 1 Month
Posted
[no name] 22-Aug-12 8:35am

Assuming the hour on both dates is 00:00, the difference is 30 days. In case of August, it's not 1 month.

Rate this:

## Solution 1

You can use subtract on the dates and specify the result you want (hours, days, etc...). Have a look here:

Good luck!
Manas Bhardwaj 17-Aug-12 8:22am

Correct +5
Member 13409121 14-Sep-17 22:07pm

How to this in vb.net
E.F. Nijboer 18-Sep-17 12:27pm

Simply use the Substract in vb.net instead of the minus (-) operator.
d1 - d2 -> d1.Subtract(d2)
Rate this:

## Solution 6

See the search results on CP:
Search Result 1[^]
Search Result 2[^]
Rate this:

## Solution 2

```static void Main(string[] args)
{
DateTime dt1 = DateTime.Parse("5/12/2011");
DateTime dt2 = DateTime.Parse("7/12/2012");
int days = (dt2 - dt1).Days;
Console.WriteLine(days);

double month = (dt2 - dt1).Days /30;
Console.WriteLine(month);
double year = (dt2 - dt1).Days / 365;
Console.WriteLine(year);

}
```
Robymon 17-Aug-12 4:08am

It is working for other Months except February.
Santhosh Kumar Jayaraman 17-Aug-12 4:32am

check this to know how to calculate month between 2 dates for february.
http://www.codeproject.com/Articles/28837/Calculating-Duration-Between-Two-Dates-in-Years-Mo
Manas Bhardwaj 17-Aug-12 8:22am

Correct +5
Rate this:

Rate this:

## Solution 3

```DateTime oldDate = new DateTime(2012, 8, 1);
DateTime newDate = new DateTime(2013, 8, 31);

// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;
int years = (int)(ts.Days / 365.25);
int months = ts.Days/ 31;
```
Robymon 17-Aug-12 4:15am

It is working for other Months except February.
ridoy 17-Aug-12 4:18am

For February you need to implement some other code..Try it..it wouldn't be much harder..
Rate this:

Rate this:

v2
Rate this:

## Solution 9

```// to get the difference between two dates
public static void GetDifference(DateTime date1, DateTime date2, out int Years, out int Months, out int Weeks, out int Days)
{
//assumes date2 is the bigger date for simplicity
//----------------------------------------------
//years
TimeSpan diff = date2 - date1;
Years = diff.Days / 366;
DateTime workingDate = date1.AddYears(Years);
while (workingDate.AddYears(1) <= date2)
{
Years++;
}
//---------------------------------------------
//months
diff = date2 - workingDate;
Months = diff.Days / 31;
while (workingDate.AddMonths(1) <= date2)
{
Months++;
}
//---------------------------------------------
//weeks and days
diff = date2 - workingDate;
Weeks = diff.Days / 7; //weeks always have 7 days
Days = diff.Days % 7;
//---------------------------------------------
}
```
Rate this:

## Solution 8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 150 phil.o 75 Patrice T 70 CPallini 60 Richard MacCutchan 55
 OriginalGriff 4,797 phil.o 3,638 Maciej Los 2,728 Richard Deeming 2,333 Richard MacCutchan 2,310

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100