c# - 格式异常毫米:ss

标签 c# timespan formatexception

我想从格式为“MM:SS”的字符串中提取 TotalSeconds。例如:01:20 我观察 80(秒)

我这样做了,但出现了异常:

TimeSpan.ParseExact(time.ToString(), "mm:ss", System.Globalization.CultureInfo.CurrentCulture).TotalSeconds;

我做错了什么?

谢谢!

最佳答案

尝试以下操作:

TimeSpan.ParseExact(time.ToString(), "mm\\:ss", System.Globalization.CultureInfo.CurrentCulture).TotalSeconds;

了解有关 Costum TimeSpan 格式的更多信息,请访问 MSDN

反斜杠作为转义字符。这意味着,在 C# 中,格式字符串必须用 @ 引号括起来,或者 mm:ss 必须用反斜杠分隔。

关于c# - 格式异常毫米:ss,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20242134/

相关文章:

c# - 将 DBNULL 值解析为 double

.net - 为什么FormatException不继承自ArgumentException?

c# - dapper.contrib postres 42P01 错误 : relation "<table name>" does not exist

c# - 如何反序列化 protobuf.net 中的 UDPTunnel

c# - c-sharp 获取一个时间跨度内的工作时间

c# - 如何将字符串解析为时间跨度

c# - 系统格式异常 : Index (zero based) must be greater than or equal to zero and less than the size of the argument list

c# - 在基类和派生类中运行相同的方法

c# - Automapper - 有条件地添加到列表

c# - 32 位和 64 位 Windows 之间的日期时间粒度