Display equivalent of “MMM d, yyyy” in current culture info

浪子不回头ぞ 提交于 2019-12-11 01:43:06

问题


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.



来源:https://stackoverflow.com/questions/37577006/display-equivalent-of-mmm-d-yyyy-in-current-culture-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!