我正在寻找 .net 3.5 中的解决方案,我编写了以下工作解决方案:
private string FormatTimeSpan(TimeSpan time)
{
return String.Format("{0}{1:00}:{2:00}", time < TimeSpan.Zero ? "-" : "", Math.Abs(time.Minutes), Math.Abs(time.Seconds));
}
但我的问题是:有没有更好的方法?也许在我不需要辅助函数的地方更短。
我正在寻找 .net 3.5 中的解决方案,我编写了以下工作解决方案:
private string FormatTimeSpan(TimeSpan time)
{
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");
}
对于低于 .Net 4.0
您可以使用:
get { return Time.ToString().Substring(3); }