How to get a Long Date format from DateTime without a weekday

Viewed 25217

I'm looking for a locale-aware way of acquiring a long date time without the weekday. Just such a beast exist?

Below is the code I use to get the long date format including the weekday:

DateTime time = ...
String formattedDate = time.ToLongDateString();

Edit

Examples of what I would like to see:

  • en-us: December 5, 2009
  • fr-fr: 5 décembre 2009
  • es-es: 05 de diciembre de 2009

ToLongDateString() returns the following:

  • en-us: Saturday, December 5, 2009
  • fr-fr: samedi 5 décembre 2009
  • es-es: sábado, 05 de diciembre de 2009
16 Answers

Improving on the accepted answer, GetAllDateTimePatterns can take a parameter to restrict the result to patterns relating to a standard format string, such as 'D' for the long date pattern.

For example, GetAllDateTimePatterns('D') is currently returning this for en-US:

"dddd, MMMM d, yyyy", "MMMM d, yyyy", "dddd, d MMMM, yyyy", "d MMMM, yyyy" 

and this for zh-HK:

"yyyy'年'M'月'd'日'", "yyyy'年'MM'月'dd'日'", "yyyy年MMMd日", "yyyy年MMMd日, dddd"

Assuming they're listed in some order of preference or prevalence, we can select the first option that does not contain the day of the week.

Here are extension methods so you can simply use myDateTime.ToLongDateStringWithoutDayOfWeek() and myDateTime.ToLongDateTimeStringWithoutDayOfWeek().

public static string ToLongDateStringWithoutDayOfWeek(this DateTime d)
{
    return d.ToString(
               CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns('D')
                   .FirstOrDefault(a => !a.Contains("ddd") && !a.Contains("dddd")) 
               ?? "D");
}

public static string ToLongDateTimeStringWithoutDayOfWeek(this DateTime d, bool includeSeconds = false)
{
    char format = includeSeconds ? 'F' : 'f';
    return d.ToString(
               CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns(format)
                   .FirstOrDefault(a => !a.Contains("ddd") && !a.Contains("dddd")) 
               ?? format.ToString()); 
}

This is an improved answer from Reza, which does not work correctly with some localizations.

string formattedDate = DateTime.Now.ToLongDateString()
   .Replace(DateTimeFormatInfo.CurrentInfo.GetDayName(DateTime.Now.DayOfWeek), "")
   .TrimStart(", ".ToCharArray());

You can concatenate the standard month/day pattern (m) with custom year (yyyy):

string formattedDate = date.ToString("M") + ", " + date.ToString("yyyy");

Examples:

June 15, 2020 (en-US)
15. juni, 2020 (da-DK)
15 Juni, 2020 (id-ID)

Kinda hilarious how many answers this question has (and that it's still an issue). Might as well join the party with my solution:

    CultureInfo culture = CultureInfo.CurrentCulture; // or replace with the culture of your choosing

    string pattern = culture.DateTimeFormat.LongDatePattern
        .Replace("dddd, ", "")
        .Replace(",dddd", "");

    return dt.ToString(pattern, culture);

Then you've got both cases covered whether the day of the week precedes or follows the rest.

Related