Created
August 12, 2013 09:42
-
-
Save harboe/6209553 to your computer and use it in GitHub Desktop.
DateTime Extensions: Display a pretty print of the duration since the original DateTime and Now.
For instance: Yesterday, 1min ago. 2weeks, etc.
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
using System; | |
public static class DateTimeExtensions | |
{ | |
public static string ToPrettyPast(this DateTime d) | |
{ | |
var timeSpan = DateTime.Now.Subtract(d); | |
if (timeSpan.Days == 1) | |
return "Yesterday."; | |
else if (timeSpan.Days >= 365) | |
{ | |
int years = (int)timeSpan.Days / 365; | |
return string.Format("{0} year{1} ago.", years, years > 1 ? "s" : string.Empty); | |
} | |
else if (timeSpan.Days >= 31) | |
{ | |
int months = (int)timeSpan.Days / 30; | |
return string.Format("{0} month{1} ago.", months, months > 1 ? "s" : string.Empty); | |
} | |
else if (timeSpan.Days > 7 && timeSpan.Days < 31) | |
{ | |
int weeks = (int)timeSpan.Days / 7; | |
return string.Format("{0} week{1} ago.", weeks, weeks > 1 ? "s" : string.Empty); | |
} | |
else if (timeSpan.Days > 0) | |
return string.Format("{0} day{1} ago.", timeSpan.Days, timeSpan.Days > 1 ? "s" : string.Empty); | |
else if (timeSpan.Hours > 0) | |
return string.Format("{0} hour{1} ago.", timeSpan.Hours, timeSpan.Hours > 1 ? "s" : string.Empty); | |
else if (timeSpan.Minutes > 0) | |
return string.Format("{0} min{1} ago.", timeSpan.Minutes, timeSpan.Minutes > 1 ? "s" : string.Empty); | |
return "Just now."; | |
} | |
} |
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
[TestClass] | |
public class DatetimeExtensionTests | |
{ | |
[TestMethod] | |
public void Test_Now() | |
{ | |
Assert.AreEqual("Just now.", DateTime.Now.ToPrettyPast()); | |
} | |
[TestMethod] | |
public void Test_Miniuts() | |
{ | |
Assert.AreEqual("1 min ago.", DateTime.Now.AddMinutes(-1).ToPrettyPast()); | |
Assert.AreEqual("2 mins ago.", DateTime.Now.AddMinutes(-2).ToPrettyPast()); | |
} | |
[TestMethod] | |
public void Test_Days() | |
{ | |
Assert.AreEqual("Yesterday.", DateTime.Now.AddDays(-1).ToPrettyPast()); | |
Assert.AreEqual("2 days ago.", DateTime.Now.AddDays(-2).ToPrettyPast()); | |
Assert.AreEqual("6 days ago.", DateTime.Now.AddDays(-6).ToPrettyPast()); | |
Assert.AreEqual("7 days ago.", DateTime.Now.AddDays(-7).ToPrettyPast()); | |
} | |
[TestMethod] | |
public void Test_Weeks() | |
{ | |
Assert.AreEqual("1 week ago.", DateTime.Now.AddDays(-8).ToPrettyPast()); | |
Assert.AreEqual("2 weeks ago.", DateTime.Now.AddDays(-15).ToPrettyPast()); | |
Assert.AreEqual("3 weeks ago.", DateTime.Now.AddDays(-22).ToPrettyPast()); | |
Assert.AreEqual("4 weeks ago.", DateTime.Now.AddDays(-29).ToPrettyPast()); | |
} | |
[TestMethod] | |
public void Test_Month() | |
{ | |
Assert.AreEqual("1 month ago.", DateTime.Now.AddMonths(-1).ToPrettyPast()); | |
Assert.AreEqual("2 months ago.", DateTime.Now.AddMonths(-2).ToPrettyPast()); | |
Assert.AreEqual("3 months ago.", DateTime.Now.AddMonths(-3).ToPrettyPast()); | |
Assert.AreEqual("4 months ago.", DateTime.Now.AddMonths(-4).ToPrettyPast()); | |
Assert.AreEqual("1 year ago.", DateTime.Now.AddMonths(-12).ToPrettyPast()); | |
} | |
[TestMethod] | |
public void Test_Year() | |
{ | |
Assert.AreEqual("1 year ago.", DateTime.Now.AddYears(-1).ToPrettyPast()); | |
Assert.AreEqual("2 years ago.", DateTime.Now.AddYears(-2).ToPrettyPast()); | |
Assert.AreEqual("3 years ago.", DateTime.Now.AddYears(-3).ToPrettyPast()); | |
Assert.AreEqual("4 years ago.", DateTime.Now.AddYears(-4).ToPrettyPast()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment