将时间跨度格式化为在小时和分钟之前为零

Format timespan to have zero before hour and minute

如何在 VS2012 中的小时或分钟之前添加零。

我正在将秒转换为小时和分钟。这是我处理 hours > 24

的代码
 Dim x As New TimeSpan(0, 0, 96000)
 MsgBox(String.Format("{0}hr:{1}min", CInt(Math.Truncate(x.TotalHours)), x.Minutes))

它工作正常,我得到以下结果:

26hr:40min

如果秒数是 3600,那么我会得到 1hr:0min,有什么办法可以按照以下格式得到它:

01hr:00min

Is there any way to have it in following format: 01hr:00min

是的,您可以在格式字符串中使用 {0:00}。第一个零是参数的索引(通常在 String.Format 中),另外两个零是占位符:

String.Format("{0:00}hr:{1:00}min", CInt(x.TotalHours), x.Minutes)

文档:The "0" Custom Specifier