C# 将TimeSpan格式设置为mm:ss,用于正时间跨度和负时间跨度

C# 将TimeSpan格式设置为mm:ss,用于正时间跨度和负时间跨度,c#,time,formatting,string-formatting,C#,Time,Formatting,String Formatting,我正在寻找.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(TimeSpan时间) { 返回Stri

我正在寻找.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(TimeSpan时间)
{
返回String.Format(“{0}{1:00}:{2:00}”,time
但我的问题是:有没有更好的办法?在我不需要帮助函数的情况下,可以使用更短的函数。

稍微短一点,使用:

私有字符串格式TimeSpan(TimeSpan时间)
{
返回((时间适用于以下.Net 4.0

您可以使用:

get { return Time.ToString().Substring(3); }

负号不应该从time.ToString()开始自动出现吗?@Tisho-它不会自动出现,而不是使用自定义格式字符串。它没有自定义格式说明符。我可以肯定,但是time.ToString(@“mm \:ss”)是否适用于.net 3.5?我刚刚想到将其作为扩展实现,使功能可以在我的程序中使用。@Tarion:@“mm \:ss”也不适用于我。始终返回“00:00”。有人提供线索吗?我认为这是一种聪明的黑客行为。我建议不要使用这种方法,因为它依赖于默认的
tostring
行为,而这种行为依赖于文化。
get { return Time.ToString().Substring(3); }