使用 String.Format 自定义格式时间跨度 [英] Custom format Timespan with String.Format
本文介绍了使用 String.Format 自定义格式时间跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将时间跨度格式化为这样的格式 49 hr 34 mn 20 sec
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}", theTimeSpan.TotalHours, theTimeSpan.Minutes, theTimeSpan.Seconds)
它将时间跨度格式化为此格式 49:34:20.如何将 hr mn sec 添加到上面的 String.Format 中?或者还有另一种简单的方法?谢谢.
It formats the Timespan to this format 49:34:20. How can I add hr mn sec to the String.Format above? or there's another easy way? Thank you.
推荐答案
很简单:
String.Format("{0:00} hr {1:00} mn {2:00} sec ", _
Math.Truncate(theTimeSpan.TotalHours), _
theTimeSpan.Minutes, theTimeSpan.Seconds)
熟悉字符串格式在 .NET 中的工作原理 - 这是一个重要的话题.
It's worth becoming familiar with how string formatting works in .NET - it's an important topic.
遗憾的是 TimeSpan
目前不支持自定义格式字符串 - 但它会从 .NET 4.0 开始!
It's unfortunate that TimeSpan
doesn't support custom format strings at the moment - but it will as of .NET 4.0!
这篇关于使用 String.Format 自定义格式时间跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文