Formatting TimeSpan in Different Cultures

I am working on an application that can be seen in many countries of the world. There are not many countries that show hours, minutes and seconds with something else except: as a seperator, but there are several, and I want to make sure that the times are formatted correctly for their region. DateTime works fine, but TimeSpan doesn't. These fragments are from my direct window in Visual Studio 2010 using .Net 4 with my region set to Malayalam (India). The call to dateTime.Now also reflects the time displayed on my watch, 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 associated with the current culture is used. If the format is the usual format format, the formatProvider parameter is ignored." Of course, I don’t even need to go to 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 other other features, it should automatically reflect them too - basically TimeSpan should be Culturally conscious , just like DateTime.

But:

 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 am looking for a simple, single line to output timeSpan in a culturally-conscious manner. Any ideas are welcome.

+7
source share
3 answers

Looks like an error, you can report it on connect.microsoft.com. Meanwhile, a workaround is to use 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:

02/00/09

+4
source

This is more like a comment, but it takes some space, so I am writing it as an answer.

Although DateTime string formatting has been in .NET for a very long time, TimeSpan formatting has been new in .NET 4.0 (Visual Studio 2010).

The culture has a DateTimeFormatInfo object that is used by DateTime and includes information about whether to use a colon : or period . or something else between hours, minutes and seconds. Now, TimeSpan does not seem to use this DateTimeFormatInfo object, and nothing is called "TimeSpanFormatInfo".

Here is an example:

 // we start from a non-read-only invariant culture Thread.CurrentThread.CurrentCulture = new CultureInfo(""); // change time separator of DateTime format info of the culture CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator = "<-->"; var dt = new DateTime(2013, 7, 8, 13, 14, 15); Console.WriteLine(dt); // writes "07/08/2013 13<-->14<-->15" var ts = new TimeSpan(13, 14, 15); Console.WriteLine(ts); // writes "13:14:15" 
+1
source

I am looking for a simple, single line to output the timeSpan according to the culture.

Then I think you better use the DateTime class to format for you:

 string display = new DateTime(timespan.Ticks).ToLongTimeString(); 

Assuming timespan has a positive duration of 0 to 24 hours.

+1
source

All Articles