.Net Core 跨平台 DateTime 中文 上午 下午 解决方案

        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now);
            Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN", true) { DateTimeFormat = { ShortDatePattern = "yyyy-MM-dd", FullDateTimePattern = "yyyy-MM-dd HH:mm:ss", LongTimePattern = "HH:mm:ss" } };
            Console.WriteLine(DateTime.Now);
            Task.Run(() => { Console.WriteLine(DateTime.Now); });
            Console.WriteLine("Hello World!");
        }

已解决。

解决方案二,之前是用的当前线程,问题,就是如果不是当前线程怎么办。

所以,提供了一个全局的默认设置

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("系统本身: " + DateTime.Now);
            CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("zh-CN", true) { DateTimeFormat = { ShortDatePattern = "yyyy-MM-dd", FullDateTimePattern = "yyyy-MM-dd HH:mm:ss", LongTimePattern = "HH:mm:ss" } };
            Console.WriteLine("设置环境后: " + DateTime.Now);
            Task.Run(() =>
            {
                Console.WriteLine("设置环境后,Task线程: " + DateTime.Now);
            });
            Thread thread = new Thread(new ThreadStart(Run));
            thread.IsBackground = true;
            thread.Start();

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
        static void Run()
        {
            Console.WriteLine("设置环境后,Thread线程: " + DateTime.Now);
        }
    }

结果:

 

来实现区域化设置 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章