Custom format Timespan with String.Format

前端 未结 1 761
北恋
北恋 2021-01-14 13:50

I want to format the Timespan to have format like this 49 hr 34 mn 20 sec

I used the String format below:

String.Format(\"{0:00}:{1:00}:{2:00}\", the         


        
相关标签:
1条回答
  • That's simple:

    String.Format("{0:00} hr {1:00} mn {2:00} sec ", _
                  Math.Truncate(theTimeSpan.TotalHours), _
                  theTimeSpan.Minutes, theTimeSpan.Seconds)
    

    It's worth becoming familiar with how string formatting works in .NET - it's an important topic.

    It's unfortunate that TimeSpan doesn't support custom format strings at the moment - but it will as of .NET 4.0!

    0 讨论(0)
提交回复
热议问题