Reputation: 2939
We are creating an application that will be shipped through the whole world, while importing and exporting files of different formats.
We should use the Invariant Culture for these standard files.
However, the invariant culture is just like the EN-US culture, instead of really providing a standard international way of writing.
That being said, I'd like to change at least the date format for this culture. But the following code just throws exceptions:
CultureInfo format = System.Globalization.CultureInfo.InvariantCulture;
format.DateTimeFormat.FullDateTimePattern = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
return format;
What would be the correct way to have an IFormatProvider that formats any kind of variable, but has an international way of writing dates?
Upvotes: 4
Views: 8859
Reputation: 7592
The invariant culture is just that, invariant. You can't change it.
This culture is intended for .NET applications which need to save data in a way which is portable across cultures.
It is not intended that users are shown or should care about the format this data takes, it should always be converted to their particular culture before seeing the data.
As far as what you could do if you really wanted this DateTime format is to start defining a new culture (possibly based off the Invariant culture if you want).
var myStandardCulture = (CultureInfo)System.Globalization.CultureInfo.InvariantCulture.Clone();
myStandardCulture.DateTimeFormat.FullDateTimePattern = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
return myStandardCulture;
The downside of defining your own culture, is other .NET applications which try to read your files would also need to be told about all of your new culture rules rather than just accepting the InvariantCulture rules built into .NET
Upvotes: 8
Reputation: 5945
There really is no "standard" way of writing... that's not what Invariant culture means. The default behavior of most of the format methods is to use the cultural settings of the base operating system to format the date, time and numbers in a way that is most suitable to whoever is using a particular computer... ie at runtime.
Try going into the control panel on your development computer and changing the localization settings. For instance, in some cultures they use the . as the thousands separator and the , as the decimal separator.
Forcing "one international" way of doing things would be confusing to them, as they're not used to seeing what we consider standard on a daily basis.
Dates are another good example of confusion, in that many areas of the world standardize on DD-MM-YYYY instead of MM-DD-YYYY as in our country.
Upvotes: 1