Click here to Skip to main content
14,384,492 members
Rate this:
Please Sign up or sign in to vote.
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
Comments
[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:
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!
   
Comments
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:
Please Sign up or sign in to vote.

Solution 6

See the search results on CP:
Search Result 1[^]
Search Result 2[^]
   
Rate this:
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();


       }
   
Comments
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:
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;
   
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:
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;
           //---------------------------------------------
       }
   

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




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