How to format the HH:mm:ss separators of a TimeSpan in a culture-aware manner?

前端 未结 3 1282
谎友^
谎友^ 2021-01-05 05:33

I\'m working on an app that may be seen in many countries in the world. There are not many countries that show hours, minutes and seconds with something other than : as a s

3条回答
  •  梦毁少年i
    2021-01-05 06:07

    Looks like a bug, you can report it at connect.microsoft.com. Meanwhile, a workaround is to take advantage of DateTime formatting. Like this:

    using System;
    using System.Globalization;
    
    class Program {
        static void Main(string[] args) {
            var ci = CultureInfo.GetCultureInfo("ml-IN");
            System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            var ts = new TimeSpan(0, 2, 9);
            var dt = new DateTime(Math.Abs(ts.Ticks));
            Console.WriteLine(dt.ToString("HH:mm:ss"));
            Console.ReadLine();
        }
    }
    

    Output:

    00.02.09

提交回复
热议问题