Created
October 26, 2011 15:07
-
-
Save jakejscott/1316627 to your computer and use it in GitHub Desktop.
Date time extension ToVerbalTimeSince
This file contains hidden or 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; | |
namespace Devastator | |
{ | |
public static class DateTimeExtensions | |
{ | |
/// <summary> | |
/// Converts a DateTime compared to the current time into plain english for explaining how recently the datetime occurred in the past. | |
/// </summary> | |
public static string ToVerbalTimeSince(this DateTime sourceDateTime) | |
{ | |
var timeSpan = DateTime.Now.Subtract(sourceDateTime); | |
string verbalResult; | |
if (timeSpan.TotalDays / 365 >= 1) | |
{ | |
var year = Math.Round(timeSpan.TotalDays / 365); | |
if (year == 1) | |
{ | |
verbalResult = year + " year ago"; | |
} | |
else | |
{ | |
verbalResult = year + " years ago"; | |
} | |
} | |
else if (timeSpan.TotalDays / 30 >= 1) | |
{ | |
var months = Math.Round(timeSpan.TotalDays / 30); | |
if (months == 1) | |
{ | |
verbalResult = months + " month ago"; | |
} | |
else | |
{ | |
verbalResult = months + " months ago"; | |
} | |
} | |
else if (timeSpan.TotalDays / 7 >= 1) | |
{ | |
var weeks = Math.Round(timeSpan.TotalDays / 7); | |
if (weeks == 1) | |
{ | |
verbalResult = weeks + " week ago"; | |
} | |
else | |
{ | |
verbalResult = weeks + " weeks ago"; | |
} | |
} | |
else if (timeSpan.Days > 1) | |
{ | |
verbalResult = timeSpan.Days + " days ago"; | |
} | |
else if (timeSpan.Days == 1) | |
{ | |
verbalResult = "yesterday"; | |
} | |
else if (timeSpan.Hours >= 1) | |
{ | |
if (timeSpan.Hours == 1) | |
{ | |
verbalResult = timeSpan.Hours + " hour ago"; | |
} | |
else | |
{ | |
verbalResult = timeSpan.Hours + " hours ago"; | |
} | |
} | |
else if (timeSpan.Minutes >= 1) | |
{ | |
if (timeSpan.Minutes == 1) | |
{ | |
verbalResult = timeSpan.Minutes + " minute ago"; | |
} | |
else | |
{ | |
verbalResult = timeSpan.Minutes + " minutes ago"; | |
} | |
} | |
else | |
{ | |
if (timeSpan.Seconds == 0) | |
{ | |
verbalResult = "a moment ago"; | |
} | |
else if (timeSpan.Seconds == 1) | |
{ | |
verbalResult = timeSpan.Seconds + " second ago"; | |
} | |
else | |
{ | |
verbalResult = timeSpan.Seconds + " seconds ago"; | |
} | |
} | |
return !string.IsNullOrEmpty(verbalResult) ? verbalResult : sourceDateTime.ToShortDateString(); | |
} | |
} | |
} |
This file contains hidden or 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; | |
using Xunit; | |
namespace Devastator.Tests.Extensions | |
{ | |
public class DateTimeExtensionsFixture | |
{ | |
[Fact] | |
public void Should_display_a_moment_ago() | |
{ | |
DateTime.Now.AddMilliseconds(-1).ToVerbalTimeSince().ShouldEqual("a moment ago"); | |
} | |
[Fact] | |
public void Should_display_1_second_ago() | |
{ | |
DateTime.Now.AddSeconds(-1).ToVerbalTimeSince().ShouldEqual("1 second ago"); | |
} | |
[Fact] | |
public void Should_display_2_seconds_ago() | |
{ | |
DateTime.Now.AddSeconds(-2).ToVerbalTimeSince().ShouldEqual("2 seconds ago"); | |
} | |
[Fact] | |
public void Should_display_1_minute_ago() | |
{ | |
DateTime.Now.AddMinutes(-1).ToVerbalTimeSince().ShouldEqual("1 minute ago"); | |
} | |
[Fact] | |
public void Should_display_2_minutes_ago() | |
{ | |
DateTime.Now.AddMinutes(-2).ToVerbalTimeSince().ShouldEqual("2 minutes ago"); | |
} | |
[Fact] | |
public void Should_display_1_hour_ago() | |
{ | |
DateTime.Now.AddHours(-1).ToVerbalTimeSince().ShouldEqual("1 hour ago"); | |
} | |
[Fact] | |
public void Should_display_2_hours_ago() | |
{ | |
DateTime.Now.AddHours(-2).ToVerbalTimeSince().ShouldEqual("2 hours ago"); | |
} | |
[Fact] | |
public void Should_display_yesterday() | |
{ | |
DateTime.Now.AddDays(-1).ToVerbalTimeSince().ShouldEqual("yesterday"); | |
} | |
[Fact] | |
public void Should_display_2_days_ago() | |
{ | |
DateTime.Now.AddDays(-2).ToVerbalTimeSince().ShouldEqual("2 days ago"); | |
} | |
[Fact] | |
public void Should_display_1_week_ago() | |
{ | |
DateTime.Now.AddDays(-7).ToVerbalTimeSince().ShouldEqual("1 week ago"); | |
} | |
[Fact] | |
public void Should_display_2_weeks_ago() | |
{ | |
DateTime.Now.AddDays(-14).ToVerbalTimeSince().ShouldEqual("2 weeks ago"); | |
} | |
[Fact] | |
public void Should_display_1_month_ago() | |
{ | |
DateTime.Now.AddMonths(-1).ToVerbalTimeSince().ShouldEqual("1 month ago"); | |
} | |
[Fact] | |
public void Should_display_2_months_ago() | |
{ | |
DateTime.Now.AddMonths(-2).ToVerbalTimeSince().ShouldEqual("2 months ago"); | |
} | |
[Fact] | |
public void Should_display_1_year_ago() | |
{ | |
DateTime.Now.AddYears(-1).ToVerbalTimeSince().ShouldEqual("1 year ago"); | |
} | |
[Fact] | |
public void Should_display_2_years_ago() | |
{ | |
DateTime.Now.AddYears(-2).ToVerbalTimeSince().ShouldEqual("2 years ago"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment