Here is a simple extension method to convert a DateTime type to a friendly name like "Today" or "Yesterday," as you see in some file browsers.
- public static string ToFriendlyDateString(this DateTime input)
- {
- var formattedDate = string.Empty;
- if (input.Date == DateTime.Today)
- {
- formattedDate = nameof(DateTime.Today);
- }
- else
- {
- formattedDate = input.Date == DateTime.Today.AddDays(-1) ? "Yesterday" :
- input.Date > DateTime.Today.AddDays(-6) ?
- input.ToString("dddd").ToString() :
- input.ToString(CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern,
- CultureInfo.CurrentCulture);
- }
- formattedDate += " @ " + input.ToString(CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern,
- CultureInfo.CurrentCulture).ToLower();
- return formattedDate;
- }