Get Date Difference in C#

To get the difference between two dates in C# is easy given the built in functions. The method requires two DateTime variables. In the example below, we will calculate the difference between two string dates.

string startDateString = "1/10/2006";
string endDateString = "3/24/2007";
DateTime startDate = Convert.ToDateTime(startDateString);
DateTime endDate = Convert.ToDateTime(endDateString);
TimeSpan dateDifference = endDate.Subtract(startDate);
int days = dateDifference.Days;

In the example above, two random dates are assigned to strings (startDateString and endDateString). The ToDateTime function of the Convert class is utilized to convert both strings into type DateTime. Finally, the subtract function is called on the future date and the result passed to the TimeSpan class. With the TimeSpan class, the difference in days, hours, milliseconds, minutes, and seconds can be determined. Our example above example is days.

More specifically, the TimeSpan class is capable of returning to either full integers as days, hours, milliseconds, minutes, and seconds. The second option is to return as doubles full and partial days, hours, milliseconds, minutes, and seconds. Additionally, this process will work with the full DateTime format.

Share and Enjoy:
  • Digg
  • DotNetKicks
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • MySpace
  • Netvibes
  • Reddit
  • StumbleUpon
You can leave a response, or trackback from your own site.

7 Responses to “Get Date Difference in C#”

  1. kapil goyal says:

    how to calculate exact date difference
    if i enter 04/08/2008 in first masktextbox1
    then second masktextbox2 04/08/1984
    then it show 24 years 6 days in textbox1
    but actual difference only 24 years
    it show 6 days extra due to leap year.

    please send me coding with C# in desktop & web application this email id
    kgoyal25@yahoo.com
    thnks

  2. Victor says:

    Hey kapil – granted the script above is giving you the actual day difference, if you want to remove the 6 extra days, you could check each year within the range and subtract one day for each leap year. A simple if statement on each year would look something like:

    if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0)
    {
    return true; // is a leap year
    }
    return false; // is not a leap year

  3. Arvind Yadav says:

    It was very useful for me.

  4. Steve-O says:

    I searched many sites for help on the Date Difference . This was the most helpful, and easy to launch. Thanks!!

  5. Ani says:

    You wrote:
    if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0)

    I don’t really understand the 400 moment. This condition seems to contradict: how can a number divide by 400 and not divide by 100? :)
    I think 400 is redundant. It should look like:

    if (year % 4 == 0 && year % 100 != 0)

  6. Sridhar says:

    Yeah Ani,, Thats right

    if (year % 4 == 0 && year % 100 != 0)

    no need of 400 moment,,

  7. Just bought a Mythic for myself and is going to be receiving one particular for my wife quickly! It has just the correct mix of features. just brief of the total smartphone. I’m retaining an eye about the Captivate for a prospective future mobile phone..
    .costs are way as well large when released. I’ve been in large tech for 20+ years and fundamentally hack anything I personal to make it greater laptops, GPS, car navigation systems, and so on. and yes cell phones too. Inside few days I’ve owned the phone I have currently installed Opera Mini, Bolt 2.1, freecaddie, Google Maps w/GPS functionality, deleted all locked useless apps after backing them up and absolutely modified the menu program to my exact needs with no all unproductive apps.

Leave a Reply