在时间跨度字符串中只需要 HH:MM:SS [英] Need only HH:MM:SS in time span string

查看:28
本文介绍了在时间跨度字符串中只需要 HH:MM:SS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间跨度字符串

I have a time span string as

1.21:00:00

这意味着 45 小时,我需要它

it means 45 hours and i need it as

45:00:00

是否可以在 C# 中做到这一点?

is it possible to do that in c#?

推荐答案

只是添加我的答案,因为字符串格式化可以比当前的建议更容易完成.

Just adding my answer because the string formatting can be done easier than current suggestions.

var ts = TimeSpan.Parse("1.21:00:00");
string.Format("{0}:{1:mm}:{1:ss}", ts.TotalHours, ts); // 45:00:00

与 Jon 的回答不同,它不需要转义.与 Soner 的回答不同,它不需要两次传递参数.

Unlike Jon's answer it doesn't require escaping. And unlike Soner's answer, it doesn't require passing the parameter twice.

编辑

对于分数 TotalHours,您可能希望像这样对值进行下限:

For fractional TotalHours you probably want to floor the value like so:

var ts = TimeSpan.Parse("1.21:55:00");      
string.Format("{0}:{1:mm}:{1:ss}", Math.Floor(ts.TotalHours), ts);

这篇关于在时间跨度字符串中只需要 HH:MM:SS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆