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

LengthOfTime

return the length of time between the start and current date

Source

public static string LengthOfTime(this DateTime date)
{
 TimeSpan lengthOfTime = DateTime.Now.Subtract(date);
 if (lengthOfTime.Minutes == 0)
 return lengthOfTime.Seconds.ToString() + "s";
 else if (lengthOfTime.Hours == 0)
 return lengthOfTime.Minutes.ToString() + "m";
 else if (lengthOfTime.Days == 0)
 return lengthOfTime.Hours.ToString() + "h";
 else
 return lengthOfTime.Days.ToString() + "d";
}

Example

lastActivityDate.LengthOfTime();

Author: Sanction10

Submitted on: 2 sep. 2010

Language: C#

Type: System.DateTime

Views: 6654