ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

TimeElapsed

Inspiration for this extension method was another DateTime extension that determines difference in current time and a DateTime object. That one returned a string and it is more useful for my applications to have a TimeSpan reference instead. That is what I did with this extension method.

Source

public static TimeSpan TimeElapsed(this DateTime date)
{
    return DateTime.Now - date;
}

Example

var myDate = DateTime.Now;
Thread.Sleep(3000);
Console.WriteLine( myDate.TimeElapsed() );
// result -> 00:00:03

Author: jlafay

Submitted on: 3 sep 2010

Language: C#

Type: System.DateTime

Views: 3512