Display equivalent of “MMM d, yyyy” in current cul

2019-07-12 20:27发布

问题:

I've perused the standard datetime formatters that C# offers, and I can't seem to find one that matches my "short date" requirement: "May 4, 2013". I know that I can do a custom format string to accomplish the goal:

myDateTimeOffset.ToString("MMM d, yyyy");

However, this application may go to other countries. Is there a way to get the current culture's equivalent to a particular format string?

myDateTimeOffset.ToString("MMM d, yyyy", CultureInfo.CurrentUICulture);

And have it adapt to an equivalent "short date" format in the foreign culture?

回答1:

I don't think there's any default format option baked in that maches your specific requirement (D may come close). I've this overview in my bookmarks for more than 10 years now, and I find it still useful: http://samples.pdmlab.com/

Anyway, as your app may not be available for an infinite amount of languages, I think your best choice will be to use some kind of DateTime extension method and format depending on the actual language being used, if even necessary.