时间跨度格式 [英] Timespan formatting

查看:278
本文介绍了时间跨度格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/574881/how-can-i-string-format-a-timespan-object-with-a-custom-format-in-net">How我可以在.NET?

你如何优雅地格式化时间跨度说例如1小时10分钟当你拥有它声明为:

How do you elegantly format a timespan to say example "1 hour 10 minutes" when you have declared it as :

TimeSpan t = new TimeSpan(0, 70, 0);

我当然知道,你可以做一些简单的数学这一点,但我有点希望有东西在.NET来处理这对我来说 - 更复杂的场景

I am of course aware that you could do some simple maths for this, but I was kinda hoping that there is something in .NET to handle this for me - for more complicated scenarios

复制的<一个href="http://stackoverflow.com/questions/574881/how-can-i-string-format-a-timespan-object-with-a-custom-format-in-net">How我可以在.NET?

推荐答案

目前对此并没有内置的功能,你需要使用自定义的方法,是这样的:

There is no built-in functionality for this, you'll need to use a custom method, something like:

TimeSpan ts = new TimeSpan(0, 70, 0);
String.Format("{0} hour{1} {2} minute{3}", 
              ts.Hours, 
              ts.Hours == 1 ? "" : "s",
              ts.Minutes, 
              ts.Minutes == 1 ? "" : "s")

这篇关于时间跨度格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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