C# Days Elapsed From DateTime

by Sam Allen - Updated January 7, 2010

You need to find the time elapsed in days since a certain date, such as a birth date or other important date. Display the age of someone or how many days ago your site was updated. Here we see an easy way to display the time elapsed using the DateTime struct in the C# programming language.

Calculating elapsed times

The .NET framework provides several methods that are useful for this, but the syntax is sometimes hard. What we must do is initialize two DateTime structs and then subtract one from the other. The example then shows how you can get the TimeSpan.

=== Program that calculates elapsed time (C#) ===

using System;

class Program
{
    static void Main()
    {
        // Initialize a date in the past.
        // This is March 3, 2008.
        string someDate = "03-03-2008";

        // 1.
        // Parse the date and put in DateTime object.
        DateTime startDate = DateTime.Parse(someDate);

        // 2.
        // Get the current DateTime.
        DateTime now = DateTime.Now;

        // 3.
        // Get the TimeSpan of the difference.
        TimeSpan elapsed = now.Subtract(startDate);

        // 4.
        // Get number of days ago.
        double daysAgo = elapsed.TotalDays;
        Console.WriteLine("{0} was {1} days ago",
            someDate,
            daysAgo.ToString("0"));
    }
}

=== Output of the program ===

3-03-2008 was 247 days ago

The example code uses DateTime.Parse. This is not strictly part of the solution, but we must have a DateTime from the past. In the example, we create the DateTime of March 3.

The code uses DateTime.Now. This is how we can get the current day and time. DateTime.Now is extremely useful in many parts of .NET. Look at how we take the DateTime.Now and call Subtract on it, passing it the earlier date. Subtract the smaller, earlier date from the larger, later date.

Using TimeSpan. TimeSpan is used with TotalDays. Subtract returned a TimeSpan. We must use the TotalDays property now to get the entire length in days in the time span. The Days property will only return the component of the time, not the total days.

(See TimeSpan Examples.)

More details

Look at how we call daysAgo.ToString("0") on double returned by TotalDays. We do this because TotalDays has a decimal, which we don't care about for the display. We want to display "145 days" instead of "144.9983" days.

Summary

Here we saw how you can find elapsed days in the C# language. View more DateTime articles at this site for more useful methods. Use the code here for an accurate method of getting the time elapsed. DateTime in C# is simple, but we just need to know the right methods.

(Do not copy this page.)

Dot Net Perls | Search
DateTime | DateTime Examples | DateTime Format Examples | DateTime Null, Using DateTime.MinValue Default | DateTime.Parse String Method | DateTime.ParseExact Usage
C# | Obsolete Attribute | True and False | Gradient Tips | Catch Examples
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen