Created
November 4, 2010 06:02
-
-
Save haileys/662172 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string Relative(this DateTime dt) | |
{ | |
var ts = DateTime.Now.ToUniversalTime() - dt; | |
if (ts.TotalMinutes < 1) | |
return string.Format("just now"); | |
if (ts.TotalMinutes < 60) | |
return string.Format("{0} minute{1} ago", (int)ts.TotalMinutes, (int)ts.TotalMinutes != 1 ? "s" : ""); | |
if (ts.TotalHours < 24) | |
return string.Format("{0} hour{1} ago", (int)ts.TotalHours, (int)ts.TotalHours != 1 ? "s" : ""); | |
if (ts.TotalDays < 30) | |
return string.Format("{0} day{1} ago", (int)ts.TotalDays, (int)ts.TotalDays != 1 ? "s" : ""); | |
return string.Format("{0:dd/MM/yyyy}", dt); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment