How can I String.Format a TimeSpan object with a custom format in .NET?

后端 未结 19 1279
情深已故
情深已故 2020-11-22 12:25

What is the recommended way of formatting TimeSpan objects into a string with a custom format?

19条回答
  •  悲哀的现实
    2020-11-22 12:50

    For .NET 3.5 and lower you could use:

    string.Format ("{0:00}:{1:00}:{2:00}", 
                   (int)myTimeSpan.TotalHours, 
                        myTimeSpan.Minutes, 
                        myTimeSpan.Seconds);
    

    Code taken from a Jon Skeet answer on bytes

    For .NET 4.0 and above, see DoctaJonez answer.

提交回复
热议问题