Created
March 8, 2014 17:48
-
-
Save curzona/9435802 to your computer and use it in GitHub Desktop.
DateTime to simple string descriptions like Today, Tomorrow, Someday, 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
// Extended DateTime constants and strinify | |
class DateTimeEx | |
{ | |
public static DateTime NextYear { get { return DateTime.Today.AddDays(-DateTime.Today.DayOfYear + 1).AddYears(1); } } | |
public static DateTime NextMonth { get { return DateTime.Today.AddDays(-DateTime.Today.Day + 1).AddMonths(1); } } | |
public static DateTime NextWeek { get { return DateTime.Today.AddDays(7 - (DateTime.Today.DayOfWeek - CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)); } } | |
public static DateTime Tomorrow { get { return DateTime.Today.AddDays(1); } } | |
public static DateTime Now { get { return DateTime.Now; } } | |
public static DateTime Today { get { return DateTime.Today; } } | |
public static DateTime Yesterday { get { return DateTime.Today.AddDays(-1); } } | |
public static DateTime Monday { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - DayOfWeek.Monday)); } } | |
public static DateTime Tuesday { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - DayOfWeek.Tuesday)); } } | |
public static DateTime Wednesday { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - DayOfWeek.Wednesday)); } } | |
public static DateTime Thursday { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - DayOfWeek.Thursday)); } } | |
public static DateTime Friday { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - DayOfWeek.Friday)); } } | |
public static DateTime Saturday { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - DayOfWeek.Saturday)); } } | |
public static DateTime Sunday { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - DayOfWeek.Sunday)); } } | |
public static DateTime ThisWeek { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)); } } | |
public static DateTime LastWeek { get { return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek - CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) - 7); } } | |
public static DateTime ThisMonth { get { return DateTime.Today.AddDays(-DateTime.Today.Day + 1); } } | |
public static DateTime LastMonth { get { return DateTime.Today.AddDays(-DateTime.Today.Day + 1).AddMonths(-1); } } | |
public static DateTime ThisYear { get { return DateTime.Today.AddDays(-DateTime.Today.DayOfYear + 1); } } | |
public static DateTime LastYear { get { return DateTime.Today.AddDays(-DateTime.Today.DayOfYear + 1).AddYears(-1); } } | |
private static List<Tuple<DateTime, string>> DefaultMapping { get; set; } | |
static DateTimeEx() | |
{ | |
DefaultMapping = new List<Tuple<DateTime, string>>(); | |
DefaultMapping.Add(new Tuple<DateTime, string>(NextYear.AddYears(1), "Someday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(NextYear, "Next Year")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(NextMonth, "NextMonth")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(NextWeek, "NextWeek")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Tomorrow, "Tomorrow")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Today, "Today")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Yesterday, "Yesterday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Monday, "Monday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Tuesday, "Tuesday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Wednesday, "Wednesday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Thursday, "Thursday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Friday, "Friday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Saturday, "Saturday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(Sunday, "Sunday")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(ThisWeek, "This Week")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(ThisMonth, "This Month")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(LastMonth, "Last Month")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(ThisYear, "This Year")); | |
DefaultMapping.Add(new Tuple<DateTime, string>(LastYear, "Last Year")); | |
} | |
public static string ToString(DateTime dateTime, List<Tuple<DateTime, string>> mapping) | |
{ | |
foreach (Tuple<DateTime, string> pair in mapping) | |
{ | |
if (dateTime >= pair.Item1) | |
{ | |
return pair.Item2; | |
} | |
} | |
return null; | |
} | |
public static string ToString(DateTime dateTime) | |
{ | |
return ToString(dateTime, DefaultMapping); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment