Change the default DateTime format to yyyy-MM-dd

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.

Thanks for the help :slight_smile:

I suggest using this definition from the Micro Framework:

2 Likes

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 -


class idontknow : whatever

I like that class :)))

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:



            CultureInfo[] cultures =
            CultureInfo.GetCultures(CultureTypes.AllCultures);
            foreach (CultureInfo culture in cultures) Debug.Print("Culture = "
            + culture.Name);

            Debug.Print("Current UI Culture='" +
            CultureInfo.CurrentUICulture.Name + "'");
            ResourceUtility.SetCurrentUICulture(new CultureInfo("fr-FR"));
            Debug.Print("Current UI Culture='" +
            CultureInfo.CurrentUICulture.Name + "'");

With further searching, there seem to be a way to add another culture as a ā€˜satelliteā€™ resource file, and deploy it along with the code, so the CultureInfo object can be createdā€¦ But I canā€™t find how to do thisā€¦ here is the thread :http://microsoft.public.dotnet.framework.microframework.free-usenet.eu/ResourceManager.SetCurrentUICulture-not-woring-in-version-4.0_T56310904_S1

Woot I got it to work with the help of the zip files in that thread: https://netmf.codeplex.com/workitem/991

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

Itā€™s ok, seems to do what I need for now thanks :slight_smile: