Click here to Skip to main content
12,448,164 members (71,837 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#2.0 C# ASP.NET
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
Comments
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: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can use subtract on the dates and specify the result you want (hours, days, etc...). Have a look here:
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/DateDiff_CS_DC09132006172429PM/DateDiff_CS_DC.aspx[^]

Good luck!
  Permalink  
Comments
Manas Bhardwaj 17-Aug-12 8:22am
   
Correct +5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

See the search results on CP:
Search Result 1[^]
Search Result 2[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

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);
           Console.Read();
 

       }
  Permalink  
Comments
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: bad
 
good
Please Sign up or sign in to vote.

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;
  Permalink  
Comments
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: bad
 
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

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)
           {
               workingDate = workingDate.AddYears(1);
               Years++;
           }
           //---------------------------------------------
           //months
           diff = date2 - workingDate;
           Months = diff.Days / 31;
           workingDate = workingDate.AddMonths(Months);
           while (workingDate.AddMonths(1) <= date2)
           {
               workingDate = workingDate.AddMonths(1);
               Months++;
           }
           //---------------------------------------------
           //weeks and days
           diff = date2 - workingDate;
           Weeks = diff.Days / 7; //weeks always have 7 days
           Days = diff.Days % 7;
           //---------------------------------------------
       }
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 19 Mar 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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