Home > Article > Backend Development > What is the best way to convert seconds to (hours:minutes:seconds:milliseconds) time in C#?
DateTime is a value type structure, similar to int, double, etc. It is available in the System namespace and is present in the mscorlib.dll assembly. It implements interfaces such as IComparable, IFormattable, IConvertible, ISerializable, IComparable, and IEquatable. DateTime contains Day, Month, Year, Hour, Minute, Second, DayOfWeek and other attributes in a DateTime object.
The TimeSpan structure represents the time interval between two times, expressed in days, hours, minutes and seconds. TimeSpan is used to compare two DateTime objects to find the difference between two dates. The TimeSpan class provides FromDays, FromHours, FromMinutes, FromSeconds and FromMilliseconds methods for creating TimeSpan objects from days, hours, minutes, seconds and milliseconds respectively.
static void Main(string[] args){ TimeSpan t = TimeSpan.FromSeconds(3752); string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); System.Console.WriteLine(answer); Console.ReadLine(); }
01h:02m:32s:000ms
static void Main(string[] args){ TimeSpan t = TimeSpan.FromSeconds(6); string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds); System.Console.WriteLine(answer); Console.ReadLine(); }
00h:00m:06s:000ms
The above is the detailed content of What is the best way to convert seconds to (hours:minutes:seconds:milliseconds) time in C#?. For more information, please follow other related articles on the PHP Chinese website!