Override date formatting for a culture in .net

2019-06-03 16:04发布

问题:

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