将TimeSpan格式设置为mm:ss,用于正和负TimeSpans [英] Format TimeSpan to mm:ss for positive and negative TimeSpans

查看:161
本文介绍了将TimeSpan格式设置为mm:ss,用于正和负TimeSpans的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人字符串FormatTimeSpan(TimeSpan)时间)
{
return String.Format({0} {1:00}:{2:00},time< TimeSpan.Zero? - :,Math.Abs (time.Minutes),Math.Abs​​(time.Seconds));
}

但是我的问题是:有更好的方法吗?也许更短的地方,我不需要一个辅助函数。

解决方案

有些更短,使用自定义TimeSpan格式字符串

  private string FormatTimeSpan(TimeSpan time)
{
return((time< TimeSpan.Zero)? - :)+ time.ToString(@mm \: SS);
}


I'm looking for a solution in .net 3.5 I wrote the following working solution:

private string FormatTimeSpan(TimeSpan time)
{
    return String.Format("{0}{1:00}:{2:00}", time < TimeSpan.Zero ? "-" : "", Math.Abs(time.Minutes), Math.Abs(time.Seconds));
}

But my Question is: Is there a better way? Maybe something shorter where I do not need an helper function.

解决方案

Somewhat shorter, using Custom TimeSpan Format Strings:

private string FormatTimeSpan(TimeSpan time)
{
    return ((time < TimeSpan.Zero) ? "-" : "") + time.ToString(@"mm\:ss");
}

这篇关于将TimeSpan格式设置为mm:ss,用于正和负TimeSpans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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