none
ShortDateFormat and clock RRS feed

  • Question

  • Hi,

    I'm working on a software which displays dates and times, using Calendars or grid.

    I'm trying to display them the same way the user sees them in its system clock, at lower-right corner of the screen.

    I'm on a french PC, which usually displays dd-mm-YYYY. In regional options, in tab "Formats", I've changed the Format to "English (UK)", and the first format (short date) to yyyy-MM-dd. 

    The clock now displays 2014-07-07, but when I try to get this format string through :

    - System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern

    - System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern

    ... the framework only gives me dd-MM-yyyy. 

    What's wrong with this code ? How can I retrieve the string that is displayed in the config. panel ?

    Thank you in advance for any help.

    Monday, July 7, 2014 2:45 PM

Answers

  • The CurrentCulture gave the same result.

    In the meantime I found the origin of my problem. For i18n purposes I set the Thread.CurrentThread.CurrentCulture variable to new CultureInfo("fr-FR" or others) when the application starts (I forgot this point).

    This means, once I set this variable, I cannot access the CurrentCulture of the PC anymore (or can I ? I assume this is kinda "custom culture" given the fact that it's a modified version of a standard culture).

    So I could solve my problem now, but I'm still asking about the way I could access the custom culture manually set in the control panel.


    If you set the Thread.CurrentThread.CurrentCulture property to a new CultureInfo object, this culture will be returned by the System.Globalization.CultureInfo.CurrentCulture from then on.

    But you could save the ShortDatePattern that is set in the Control Panel just before you set the Thread.CurrentThread.CurrentCulture and keep this value somewhere for the lifetime of your application:

      public class ApplicationGlobals
      {
       public static string ShortDatePattern { get; set; }
      }

    //save format from Control Panel...
    ApplicationGlobals.ShortDatePattern = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
    //specify another CultureInfo
    Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

    I am afraid that there is no .NET API that lets you access values from the Control Panel directly.

    • Marked as answer by pocky6 Thursday, July 10, 2014 12:30 PM
    Tuesday, July 8, 2014 12:43 PM

All replies

  • The System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern property should get you the format that is specified under Control Panel->Region and Language->Formats->Short date:

    Make sure that you restart your application once you have changed the fomat in the Control Panel.

    Monday, July 7, 2014 3:37 PM
  • Thank you for your answer. 

    However I restarted the application everytime I clicked "Run" on Visual Studio, I even restarted my PC to ensure the framework could reset its internal variables...

    All I get is this:

    It's in french so "a" stands for "année" = year, and "j" for "jour" = day.

    As you can see the clock and the setting match, but the ShortDatePattern doesn't.

    Tuesday, July 8, 2014 9:23 AM
  • The image you have posted a picture displays the value of the System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern property but you should get the value from the System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern property.
    Tuesday, July 8, 2014 9:28 AM
  • The CurrentCulture gave the same result.

    In the meantime I found the origin of my problem. For i18n purposes I set the Thread.CurrentThread.CurrentCulture variable to new CultureInfo("fr-FR" or others) when the application starts (I forgot this point).

    This means, once I set this variable, I cannot access the CurrentCulture of the PC anymore (or can I ? I assume this is kinda "custom culture" given the fact that it's a modified version of a standard culture).

    So I could solve my problem now, but I'm still asking about the way I could access the custom culture manually set in the control panel.

    Tuesday, July 8, 2014 12:06 PM
  • The CurrentCulture gave the same result.

    In the meantime I found the origin of my problem. For i18n purposes I set the Thread.CurrentThread.CurrentCulture variable to new CultureInfo("fr-FR" or others) when the application starts (I forgot this point).

    This means, once I set this variable, I cannot access the CurrentCulture of the PC anymore (or can I ? I assume this is kinda "custom culture" given the fact that it's a modified version of a standard culture).

    So I could solve my problem now, but I'm still asking about the way I could access the custom culture manually set in the control panel.


    If you set the Thread.CurrentThread.CurrentCulture property to a new CultureInfo object, this culture will be returned by the System.Globalization.CultureInfo.CurrentCulture from then on.

    But you could save the ShortDatePattern that is set in the Control Panel just before you set the Thread.CurrentThread.CurrentCulture and keep this value somewhere for the lifetime of your application:

      public class ApplicationGlobals
      {
       public static string ShortDatePattern { get; set; }
      }

    //save format from Control Panel...
    ApplicationGlobals.ShortDatePattern = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
    //specify another CultureInfo
    Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

    I am afraid that there is no .NET API that lets you access values from the Control Panel directly.

    • Marked as answer by pocky6 Thursday, July 10, 2014 12:30 PM
    Tuesday, July 8, 2014 12:43 PM