Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 359
Adam Tegen Avatar asked Sep 08 '09 16:09

Adam Tegen


People also ask

How can I get only the date from datetime format?

ToString() − One more way to get the date from DateTime is using ToString() extension method. The advantage of using ToString() extension method is that we can specify the format of the date that we want to fetch. DateTime. Date − will also remove the time from the DateTime and provides us the Date only.

What is FFF in date format?

The "fff" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.

What is long date format?

The long date option adds the day of the week and writes out the month. So 1/1/19 will display as Tuesday, January 1, 2019. The date will display in the format you choose, no matter how you enter it. If you type "1/1/19" into a cell with the long date format, Excel will change it to the expanded date.

What is the Z in datetime?

The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).


1 Answers

This seemed to do the trick.

  1. Enumerate all valid datetime patterns: CultureInfo.DateTimeFormat.GetAllDateTimePatterns
  2. Select longest pattern (presumably this is the best match) that:
    • Is a substring of the CultureInfo.DateTimeFormat.LongDatePattern
    • Does not contain "ddd" (short day name)
    • Does not contain "dddd" (long day name)

This appears to come up with the strings I was looking for.

See code below:

class DateTest {     static private string GetDatePatternWithoutWeekday(CultureInfo cultureInfo)     {         string[] patterns = cultureInfo e.DateTimeFormat.GetAllDateTimePatterns();          string longPattern = cultureInfo.DateTimeFormat.LongDatePattern;          string acceptablePattern = String.Empty;          foreach (string pattern in patterns)         {             if (longPattern.Contains(pattern) && !pattern.Contains("ddd") && !pattern.Contains("dddd"))             {                 if (pattern.Length > acceptablePattern.Length)                 {                     acceptablePattern = pattern;                 }             }         }          if (String.IsNullOrEmpty(acceptablePattern))         {             return longPattern;         }         return acceptablePattern;     }      static private void Test(string locale)     {         DateTime dateTime = new DateTime(2009, 12, 5);          Thread.CurrentThread.CurrentCulture  = new CultureInfo(locale);          string format = GetDatePatternWithoutWeekday(Thread.CurrentThread.CurrentCulture);          string result = dateTime.ToString(format);          MessageBox.Show(result);                 } } 

Technically, it probably wouldn't work if a long format had the name of the day sandwiched in the middle. For that, I should choose the pattern with longest common substring instead of longest exact match.

like image 116
Adam Tegen Avatar answered Sep 19 '22 01:09

Adam Tegen