DOTNET中设置自定义的日期格式

由于Thread.CurrentThread.CurrentCulture实例是只读的,所以对其的操作一般要使用clone方法

如果想使用自己系统预设的日期显示格式而不是操作系统中设置的格式,在main方法中,执行如下:

CultureInfo culinfo = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
            DateTimeFormatInfo dateInfo = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.DateTimeFormat.Clone();
            dateInfo.ShortDatePattern = "yyyy-M-d";
            dateInfo.LongDatePattern = "yyyy-MM-dd";
            dateInfo.ShortTimePattern = "HH:mm";
            dateInfo.LongTimePattern = "HH:mm:ss";
            culinfo.DateTimeFormat = dateInfo;
            Thread.CurrentThread.CurrentCulture = culinfo;

获取 当前线程区域性的cultrueInfo 和日期格式副本
对日期格式进行设置 然后把当前线程的 culinfo设置为想要的日期格式,主要是对grid里的日期起作用


// Displays the name of the CurrentCulture of the current thread.
      Console.WriteLine( "CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name );

      // Changes the CurrentCulture of the current thread to th-TH.
      Thread.CurrentThread.CurrentCulture = new CultureInfo( "th-TH", false );
      Console.WriteLine( "CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name );

      // Displays the name of the CurrentUICulture of the current thread.
      Console.WriteLine( "CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name );

      // Changes the CurrentUICulture of the current thread to ja-JP.
      Thread.CurrentThread.CurrentUICulture = new CultureInfo( "ja-JP", false );
      Console.WriteLine( "CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name );

结果显示:

CurrentCulture is en-US

.CurrentCulture is now th-TH.

CurrentUICulture is en-US.

CurrentUICulture is now ja-JP.

CurrentCulture is en-US.

CurrentCulture is now th-TH

.CurrentUICulture is en-US

.CurrentUICulture is now ja-JP.

你可能感兴趣的:(thread)