Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 seperator but there are a few and I want to make sure that times are formatted correctly for their region. DateTime is great at this but TimeSpan isn't. These snippets are from my immediate Window in Visual Studio 2010 using .Net 4 with my region set to Malayalam (India). The dateTime.Now call also reflects how time is shown on my clock, Microsoft Outlook and other areas.

DateTime.Now.ToString()
"02-10-12 17.00.58"

http://msdn.microsoft.com/en-us/library/dd784379.aspx Says "If formatProvider is null, the DateTimeFormatInfo object that is associated with the current culture is used. If format is a custom format string, the formatProvider parameter is ignored." It stands to reason then that i shouldn't need to even pass in the Current CultureInfo. The format i want here is hh.mm.ss but obvioulsy hh:mm:ss when in most other languages, and if there are anoy other poossibilities, it should automatically reflect those too - basically TimeSpan should be culture-Aware, just as DateTime is.

However:

timeRemaining.ToString()
"00:02:09"
timeRemaining.ToString("c")
"00:02:09"
timeRemaining.ToString("c", CultureInfo.CurrentCulture)
"00:02:09"
timeRemaining.ToString("g")
"0:02:09"
timeRemaining.ToString("G")
"0:00:02:09.0000000"
timeRemaining.ToString("t")
"00:02:09"
timeRemaining.ToString("g", CultureInfo.CurrentCulture)
"0:02:09"
timeRemaining.ToString("g", CultureInfo.CurrentUICulture)
"0:02:09"
timeRemaining.ToString("G", CultureInfo.CurrentUICulture)
"0:00:02:09.0000000"
timeRemaining.ToString("G", CultureInfo.CurrentCulture)
"0:00:02:09.0000000"
timeRemaining.ToString("t", CultureInfo.CurrentCulture)
"00:02:09"

I'm looking for a simple, single line to output a timeSpan in a culture-Aware manner. Any ideas are appreciated.

like image 839
CHRISTOPHER MARTIN Avatar asked Oct 02 '12 22:10

CHRISTOPHER MARTIN


People also ask

What is the format of a TimeSpan?

"c" is the default TimeSpan format string; the TimeSpan. ToString() method formats a time interval value by using the "c" format string. TimeSpan also supports the "t" and "T" standard format strings, which are identical in behavior to the "c" standard format string.

What is TimeSpan C#?

C# TimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. C# TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.


1 Answers

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

like image 187
Hans Passant Avatar answered Sep 16 '22 13:09

Hans Passant