Created
December 20, 2010 00:20
-
-
Save lukencode/747863 to your computer and use it in GitHub Desktop.
DateExtensions.cs
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 class DateExtensions | |
{ | |
public static string TimeAgo(this DateTime date) | |
{ | |
if (date <= DateTime.Now) | |
{ | |
var timeSince = DateTime.Now.Subtract(date); | |
if (timeSince.TotalMilliseconds < 1) return "not yet"; | |
if (timeSince.TotalMinutes < 1) return "just now"; | |
if (timeSince.TotalMinutes < 2) return "1 minute ago"; | |
if (timeSince.TotalMinutes < 60) return string.Format("{0} minutes ago", timeSince.Minutes); | |
if (timeSince.TotalMinutes < 120) return "1 hour ago"; | |
if (timeSince.TotalHours < 24) return string.Format("{0} hours ago", timeSince.Hours); | |
if (timeSince.TotalDays == 1) return "yesterday"; | |
if (timeSince.TotalDays < 7) return string.Format("{0} days ago", timeSince.Days); | |
if (timeSince.TotalDays < 14) return "last week"; | |
if (timeSince.TotalDays < 21) return "2 weeks ago"; | |
if (timeSince.TotalDays < 28) return "3 weeks ago"; | |
if (timeSince.TotalDays < 60) return "last month"; | |
if (timeSince.TotalDays < 365) | |
return string.Format("{0} months ago", Math.Round(timeSince.TotalDays / 30)); | |
if (timeSince.TotalDays < 730) return "last year"; //last but not least... | |
return string.Format("{0} years ago", Math.Round(timeSince.TotalDays / 365)); | |
} | |
else | |
{ | |
var timeUntil = date.Subtract(DateTime.Now); | |
if (timeUntil.TotalMilliseconds < 1) return "not yet"; | |
if (timeUntil.TotalMinutes < 1) return "just now"; | |
if (timeUntil.TotalMinutes < 2) return "1 minute"; | |
if (timeUntil.TotalMinutes < 60) return string.Format("{0} minutes", timeUntil.Minutes); | |
if (timeUntil.TotalMinutes < 120) return "1 hour"; | |
if (timeUntil.TotalHours < 24) return string.Format("{0} hours", timeUntil.Hours); | |
if ((int)timeUntil.TotalDays == 1) return "tomorrow"; | |
if (timeUntil.TotalDays < 7) return string.Format("{0} days", timeUntil.Days); | |
if (timeUntil.TotalDays < 14) return "next week"; | |
if (timeUntil.TotalDays < 21) return "in 2 weeks"; | |
if (timeUntil.TotalDays < 28) return "in 3 weeks"; | |
if (timeUntil.TotalDays < 60) return "next month"; | |
if (timeUntil.TotalDays < 365) return string.Format("{0} months", Math.Round(timeUntil.TotalDays / 30)); | |
if (timeUntil.TotalDays < 730) return "next year"; | |
return string.Format("in {0} years", Math.Round(timeUntil.TotalDays / 365)); | |
} | |
} | |
public static string ToString(this DateTime? date, string nullText, string format = "") | |
{ | |
if (date.HasValue) | |
{ | |
if (format.IsNullOrEmpty()) | |
return date.Value.ToString(); | |
return date.Value.ToString(format); | |
} | |
return nullText; | |
} | |
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) | |
{ | |
int diff = dt.DayOfWeek - startOfWeek; | |
if (diff < 0) | |
{ | |
diff += 7; | |
} | |
return dt.AddDays(-1 * diff).Date; | |
} | |
public static bool IsBetween(this DateTime date, DateTime firstDate, DateTime secondDate) | |
{ | |
return (date >= firstDate && date <= secondDate) || (date <= firstDate && date >= secondDate); | |
} | |
public static bool IsValidSqlDateTime(this DateTime date) | |
{ | |
return date.IsBetween((DateTime)System.Data.SqlTypes.SqlDateTime.MinValue, (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment