extensions: Add helper for formatting pretty dates

This commit is contained in:
Hylke Bons 2014-10-30 16:58:28 +00:00
parent 1d3b044a07
commit 5fa2da063a

View file

@ -76,5 +76,34 @@ namespace SparkleLib {
{
return ((file.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint);
}
public static string ToPrettyDate (this DateTime timestamp)
{
TimeSpan time_diff = DateTime.Now.Subtract(timestamp);
int day_diff = (int) time_diff.TotalDays;
if (day_diff == 0) {
return "at " + timestamp.ToString ("HH:mm");
} else if (day_diff == 1) {
return "yesterday at " + timestamp.ToString ("HH:mm");
} else if (day_diff < 7) {
return timestamp.ToString ("dddd");
} else if (day_diff < 31) {
if (day_diff < 14)
return "last week";
else
return string.Format ("{0} weeks ago", Math.Ceiling ((double) day_diff / 7));
} else if (day_diff < 62) {
return "last month";
} else {
return string.Format ("{0} months ago", Math.Ceiling ((double) day_diff / 31));
}
}
}
}