C# 时间跨度 ToString 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11077711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TimeSpan ToString format
提问by Aximili
Just curious, is there a format string I can use to output something like "5h 3m 30s"?
只是好奇,有没有我可以用来输出“5h 3m 30s”之类的格式字符串?
eg. (obviously wrong)
例如。(显然是错误的)
myTimeSpan.ToString("hh mm ss")
采纳答案by Jon Skeet
Try:
尝试:
myTimeSpan.ToString("h'h 'm'm 's's'")
(Note that even spaces need to be quoted - that's what was wrong with my first attempt.)
(请注意,甚至需要引用空格 - 这就是我第一次尝试的错误所在。)
I'm assuming you're using .NET 4, of course - before that, TimeSpandidn't support custom format strings.
我假设您使用的是 .NET 4,当然 - 在此之前,TimeSpan不支持自定义格式字符串。
EDIT: As noted, this won't work beyond 24 hours. Also note that alternatives are available via Noda Timetoo :)
编辑:如前所述,这不会超过 24 小时。另请注意,Noda Time也提供替代方案:)
回答by John Woo
how about concactenation:
连接怎么样:
String oTime = myTimeSpan.ToString("h") + "h " +
myTimeSpan.ToString("m") + "m " +
myTimeSpan.ToString("s") + "s "
UPDATE 1:
更新1:
You can escape it with single quote: h'h 'm'm 's's'
您可以使用单引号将其转义: h'h 'm'm 's's'
回答by Dimension
Be aware of this when using the answer from Jon Skeet, with code like this:
使用 Jon Skeet 的答案时请注意这一点,代码如下:
// 12 days, 23 hours, 24 minutes, 2 seconds.
TimeSpan span = new TimeSpan(12, 23, 24, 2);
// 27 hours, 24 minutes, 2 seconds
TimeSpan span2 = new TimeSpan(27,24,2);
string format = span.ToString("h'h 'm'm 's's'");
string format2 = span2.ToString("h'h 'm'm 's's'");
Console.WriteLine(format);
Console.WriteLine(format2);
Console.ReadLine();
You get results like these:
你会得到这样的结果:
23h 24m 2s
3h 24m 2s
The hour format can at maximum show 23 hours. It will not show 27 hours or convert the 12 days to hours, it will simply cut them off as if they never existed.
小时格式最多可以显示 23 小时。它不会显示 27 小时或将 12 天转换为小时,它会简单地将它们切断,就好像它们从未存在过一样。
One way to fix this would be to create an extension that checks the length of the TimeSpan and creates formatting based on if the timespan is over a year, day, ect. Or you could simply always show days as well because they never cut off:
解决此问题的一种方法是创建一个扩展来检查时间跨度的长度并根据时间跨度是否超过一年、一天等创建格式。或者您也可以简单地始终显示日期,因为它们永远不会中断:
string newFormat = span.ToString("d'd 'h'h 'm'm 's's'");
Do note I am a beginner at programming. This is only coming from observations after I was lucky enough to notice this after having assumed it would show all hours. I'm saying this because I don't know if there is a better solution, like another hour format that can display endless hours.
请注意,我是编程的初学者。这只是在我假设它会显示所有小时后有幸注意到这一点之后的观察结果。我这么说是因为我不知道是否有更好的解决方案,例如另一种可以显示无限小时的小时格式。
I do however think this format is doing its intended functionality. You just have to be aware of it. Thus this post. Jon Skeet's answer never indicated that this format is to show only the hour property of a date type format where the hours can be at most 23.
然而,我确实认为这种格式正在发挥其预期的功能。你只需要意识到这一点。于是有了这个帖子。Jon Skeet 的回答从未表明此格式仅显示日期类型格式的小时属性,其中小时数最多为 23。
回答by Holf
If you're unfortunate enough not to be using .NET4:
如果您不幸没有使用 .NET4:
string.Format("{0}h{1}m{2}s",
myTimeSpan.Hours,
myTimeSpan.Minutes,
myTimeSpan.Seconds);
回答by zamoldar
This little code helps, for parse and reverse back:
这个小代码有助于解析和反向返回:
var t = TimeSpan.FromMilliseconds(450780);
double d1 = t.TotalSeconds;
string t3 = t.ToString(@"hh\:mm\:ss\.f",null);
var tt = TimeSpan.ParseExact(t3, @"hh\:mm\:ss\.f",null);
double d2 = tt.TotalSeconds;
回答by Jonas Stensved
Using Xamarinand .NET Portable 4.5this was the only format I got working after trying all other answers here:
使用Xamarin,.NET Portable 4.5这是我在这里尝试所有其他答案后得到的唯一格式:
timespan.ToString("hh':'mm':'ss");
Essentially I'm putting the :in single quotes to escape them.
本质上,我将 放在:单引号中以逃避它们。
回答by Giles
I sometimes find format strings a little like regexes, in that when I come back to the code later, I've forgotten the nuances. I've therefore gone with the following, which is less likely to send me back the documentation:
有时我发现格式字符串有点像正则表达式,因为当我稍后回到代码时,我已经忘记了细微差别。因此,我采用了以下内容,这不太可能将文档发回给我:
string FormatTimeSpan(TimeSpan timeSpan) =>
$"{Math.Floor(timeSpan.TotalHours)}h {timeSpan.Minutes}m {timeSpan.Seconds}s";
String interpolation, introduced in C# 6, is making this a lot clearer than it would otherwise be.
C# 6 中引入的字符串插值使这比其他方式更清晰。

