Main Site Documentation

Change the default DateTime format to yyyy-MM-dd


#1

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:


#2

I suggest using this definition from the Micro Framework:

http://msdn.microsoft.com/en-us/library/hh401613.aspx


#3

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?


#4

@ andre.m -


class idontknow : whatever

I like that class :)))


#5

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


#6

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


#7

It’s ok, seems to do what I need for now thanks :slight_smile: