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:
ToLongDateString() returns the following:
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.
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.
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.
The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).
This seemed to do the trick.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With