Override date formatting for a culture in .net

牧云@^-^@ 提交于 2019-12-10 11:40:37

问题


We have a problem with the built-in .net date formatting. We are using the abbreviated month format:

string.Format("{0:MMM}", date);

For our Czech site, it outputs Roman numerals (eg "IV" where in en-GB it would be "Apr"). We want to change this to some custom abbreviations.

What is the best way to do this? I don't really want to put an if statement in the view. Should I use a DateTimeFormatInfo?

Also, how do Microsoft decide on the formatting, is it based on a standard?


回答1:


Which culture do you want to use? You can just specify it in the call to string.Format. For example:

string text = string.Format(CultureInfo.InvariantCulture, "{0:MMM}", date);

Note that if that's really all you're formatting, it would be simpler to call ToString:

string text = date.ToString("MMM", CultureInfo.InvariantCulture);

(That way you don't need all the composite formatting stuff.)



来源:https://stackoverflow.com/questions/4306319/override-date-formatting-for-a-culture-in-net

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