Find number of years, months and days between two dates using C#

Subscribe to the Technacular RSS feed  RSS Feed or by Email and receive free daily updates

Recently I ran into a situation where I had the start date and the end date and had to store the start date and the difference between the end and the start date (in terms of years, months, days) as string.

Here is a C# code to do that:

public static string DateDiff(DateTime startDate, DateTime endDate)
{
   string timeStr = string.Empty;
   int yr = 0;
   int mth = 0;
   int days = 0;

   TimeSpan ts = new TimeSpan();
   ts = endDate.Subtract(startDate);
   yr = (ts.Days/365);

   do
   {
       for(int i=0; i <= 12; i++)
       {
         if(endDate.Subtract(startDate.AddYears(yr).AddMonths(i)).Days > 0)
         {
             mth = i;
          }
          else
          {
             break;
          }
        }

        if(mth > 12)
           yr = yr + 1;
    }while(mth > 12);

    days = endDate.Subtract(startDate.AddYears(yr).AddMonths(mth)).Days;

    if(yr > 0)
                timeStr += yr.ToString() + "y";
    if(mth > 0)
                timeStr += mth.ToString() + "m";
    if(days > 0)
                timeStr += days.ToString() + "d";

     return(timeStr);
}
How would you have done this?
Technorati tags: , , , , ,

Popularity: 5% [?]

Subscribe to the Technacular RSS feed  RSS Feed or by Email and receive free daily updates

Related Posts

  • Monitor Blogosphere For Search Terms Using RSS Feeds
  • Google Blogsearch, a search engine dedic...
  • Moving To Bangalore: Choosing an International Moving Company
  • My wife (Archita) and I worked real hard...
  • Find the running status of trains in India
  • It is always frustrating to call the rai...
  • Complete Coverage: Moving From USA To Bangalore
  • It has been more than a month since...

    3 comments:

    1. Craig, 22. January 2009, 1:11

      Very good.. i’ve been trying to wring something the same myself, only it had some shortcomings (assumed a month had 30 days)

      I did find one minor bug in the code though.. that is wher you have yr = (ts.Days / 365).. If you have a 2 dates that are 1 year and 7 months apart (1.6…. years) yr is automatically rounded to 2 years.. To solve this you just need to have
      yr = Math.Floor(ts.Days / 365))

      My code is in VB.NET also.. so here is my converted version if anyone wants it..

      Dim timeStr As String = String.Empty
      Dim yr As Integer = 0
      Dim mth As Integer = 0
      Dim days As Integer = 0

      Dim ts As TimeSpan = New TimeSpan()
      ts = endDate.Subtract(startDate)
      yr = Math.Floor((ts.Days / 365))

      Dim monthReached As Boolean = False

      Do
      For i As Integer = 0 To 12 Step 1
      If (endDate.Subtract(startDate.AddYears(yr).AddMonths(i)).Days > 0) Then
      mth = i
      Else
      Exit Do
      End If
      Next

      If (mth > 12) Then
      yr = yr + 1
      End If
      Loop While mth > 12

      days = endDate.Subtract(startDate.AddYears(yr).AddMonths(mth)).Days

      If (yr > 0) Then
      timeStr += yr.ToString() + ” Years”
      End If

      If (mth > 0) Then
      If (yr > 0) Then
      timeStr += “, ”
      End If
      timeStr += mth.ToString() + ” Months”
      End If

      If (days > 0) Then
      If (yr > 0 Or mth > 0) Then
      timeStr += “, ”
      End If
      timeStr += days.ToString() + ” Days”
      End If

      Return timeStr

       
    2. a52, 1. April 2009, 15:02

      a faster way:
      public static int DiffFechaYear(DateTime startDate, DateTime endDate)
      {
      int iYear = (endDate.Year - startDate.Year);
      if (endDate.Year != startDate.Year)
      if (endDate.Month < startDate.Month)
      iYear = iYear - 1;
      else if (endDate.Month == startDate.Month)
      if (endDate.Day < startDate.Day)
      iYear = iYear - 1;

      return iYear;

      }

       
    3. Leedrick, 21. May 2009, 3:04

      You need to change this line:

      if(endDate.Subtract(startDate.AddYears(yr).AddMonths(i)).Days > 0)

      to this:

      if (endDate.Subtract(startDate.AddYears(yr).AddMonths(i)).Days >= 0)

      otherwise if the timespan falls exactly on a month it will only show days.

       

    Write a comment:

    Technology Blogs - Blog Top Sites Blogarama - The Blog Directory Technology blogs Technology blogs blog search directory