Dot Net Perls

Get Days Elapsed From DateTime - C#

by Sam Allen

Problem

How many days ago was a certain date? Find the time elapsed in days since a certain date, such as a birthdate or other important date. You may want to display the age of someone or how many days ago your site was updated.

Solution: C# and DateTime class

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 objects and then subtract one from the other. Then we use TimeSpan.

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"));
        //
        // "03-03-2008 was 247 days ago"
        //
    }
}
  1. DateTime.Parse is used.
    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 3rd.
  2. DateTime.Now is referenced.
    This is how we can get the current day and time. DateTime.Now is extremely useful in many parts of .NET and I am glad I know how to use it.
  3. Subtract instance method is called.
    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.
  4. 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.

Information: other C# 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: calculating elapsed days

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. [C# - DateTime Use]

Dot Net Perls
About
Sitemap
Source code
RSS
DateTime
DateTime Tips and Tricks
Use DateTime for Filenames
Pretty Date Formatting
Get Days Elapsed From DateTime
FromOADate and Excel Dates
Recent
Pi
NGEN Installer Class
List Element Equality
DateTime Tips and Tricks
Remove HTML Tags From String
© 2008 Sam Allen. All rights reserved.