覆盖日期格式在.NET中文化中文化、日期、格式、NET

2023-09-04 04:49:20 作者:情缘の风少

我们有内置.NET日期格式存在问题。我们使用的是缩写的月份格式为:

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

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

有关我们的捷克网站,它输出罗马数字(例如IV里的EN-GB这将是月)。我们要改变这一些自定义的缩写。

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.

什么是做到这一点的最好方法是什么?我真的不希望把一个if语句在视图中。我应该使用的DateTimeFormatInfo?

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?

推荐答案

哪个文化,你要使用?你只需指定它在调用的String.Format 。例如:

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);

需要注意的是,如果这是的真正的所有要格式化,它会更简单调用的ToString

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.)