Last active
April 9, 2020 13:44
-
-
Save ashour/a7097c1a4a7e677402c3a144bc2bb50d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
public static class TimeSpanExtensions | |
{ | |
public static string LocalizedTimeFormat( | |
this TimeSpan timeSpan, CultureInfo cultureInfo) | |
{ | |
string formattedTimeSpan = timeSpan.ToString(@"hh\:mm\:ss"); | |
string timeSeparator = cultureInfo.DateTimeFormat.TimeSeparator; | |
return formattedTimeSpan.Replace(":", timeSeparator); | |
} | |
} | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
var dateTime1 = new DateTime(1970, 1, 1, 0, 0, 0); | |
var dateTime2 = new DateTime(2020, 4, 1, 13, 30, 22, 500); | |
TimeSpan span = dateTime2 - dateTime1; | |
Console.WriteLine(span.LocalizedTimeFormat(new CultureInfo("en-US"))); | |
// => 13:30:22 | |
Console.WriteLine(span.LocalizedTimeFormat(new CultureInfo("fr-FR"))); | |
// => 13:30:22 | |
Console.WriteLine(span.LocalizedTimeFormat(new CultureInfo("ml-IN"))); | |
// => 13.30.22 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment