我想格式化Timespan,使其格式为49小时34分20秒
我使用下面的String格式:
String.Format("{0:00}:{1:00}:{2:00}", theTimeSpan.TotalHours, theTimeSpan.Minutes, theTimeSpan.Seconds)
Run Code Online (Sandbox Code Playgroud)
它将Timespan格式化为49:34:20格式.如何将hr mn sec添加到上面的String.Format?还是有另一种简单的方法?谢谢.
这很简单:
String.Format("{0:00} hr {1:00} mn {2:00} sec ", _
Math.Truncate(theTimeSpan.TotalHours), _
theTimeSpan.Minutes, theTimeSpan.Seconds)
Run Code Online (Sandbox Code Playgroud)
值得熟悉字符串格式在.NET中的工作方式 - 这是一个重要的主题.
不幸的是,TimeSpan
目前不支持自定义格式字符串 - 但它将从.NET 4.0开始!