Is there a way in C# Micro Framework 4.2 to change the default string format?
We would like to use the yyyy-MM-dd by default.
So if I do DateTime.Now.ToString() it would output something like 2013-08-19 12:04:22 by default, instead of 08/19/2013 12:04:22, which is a very confusing date due to month vs day.
I know I can output a DateTime in any format I want, however I need to call such a method everywhere I output a date, and is prone to error.
Well I know I can change the ToString(āyyyy-MM-ddā) method to output with the format I need. Thatās what I do already.
My problem is that for example (a common case): I have a method that returns an 'objectā
Now all I do is just thatObject.ToString() I donāt know/or I donāt want to treat it as a special case and just do a ToString().
There is a way to do this in a winform for example, by setting the GlobalCulture at the start of each threads. This way I can say the decimal point is ā,ā or ā.ā by default, or how the date is displayed/treated for parsing.
Isnāt there a way to change the way the DateTime.ToString() outputs by default in micro framework? Or that would require a recompilation of the firmware?
andre.m I think youāre on to something!
Iām trying to change the fields in DateTimeFormatInfo but it is read only, and Iām trying to create a DateTimeFormatInfo but there is no constructor.
Then I tried to change the current Culture, but there is only EN available.
Hereās a little snippet of code that lists all the cultures:
Iāve added a āsatelliteā project called mscorlib.en with my new culture resource file called en-CA.resx file, with some custom formating, then added the reference of this project to my main project.
A little helper function Iāve made to set the new culture, since ResourceUtility.SetCurrentUICulture(new CultureInfo(āen-CAā)); would not work (Iām not sure why, but iterating through all available cultures worked):
// Note, you have to add a satellite project that have a resource file with your new culture information
// By default .Net Microframework has only 'en' culture.
private static void SetUICulture(string cultureName) {
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo culture in cultures) {
if (culture.Name == cultureName) {
ResourceUtility.SetCurrentUICulture(culture);
Debug.Print(cultureName + " culture set");
break;
}
}
}
How to use:
SetUICulture("en-CA");
Now when I do a DateTime.Now.ToString(), it outputs this way:
2013-08-20 18:19:45