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

后端 未结 19 1544
情深已故
情深已故 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:56

    One way is to create a DateTime object and use it for formatting:

    new DateTime(myTimeSpan.Ticks).ToString(myCustomFormat)
    
    // or using String.Format:
    String.Format("{0:HHmmss}", new DateTime(myTimeSpan.Ticks))
    

    This is the way I know. I hope someone can suggest a better way.

提交回复
热议问题