12,448,164 members (71,837 online)
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 16-Aug-12 21:35pm
Robymon1.1K
Jani Giannoudis 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
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 J 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;
{
Years++;
}
//---------------------------------------------
//months
diff = date2 - workingDate;
Months = diff.Days / 31;
{
Months++;
}
//---------------------------------------------
//weeks and days
diff = date2 - workingDate;
Weeks = diff.Days / 7; //weeks always have 7 days
Days = diff.Days % 7;
//---------------------------------------------
}
```
Rate this:

## Solution 8

Top Experts
Last 24hrsThis month
 ppolymorphe 305 Richard Deeming 185 OriginalGriff 180 Jochen Arndt 175 F-ES Sitecore 120
 OriginalGriff 5,683 Karthik Bangalore 3,335 ppolymorphe 3,262 Maciej Los 2,872 Richard Deeming 2,695