TimeSpan.ParseExact 与毫秒

TimeSpan.ParseExact with ms

正在尝试解析以下时间

string time = "12:25:1197";
TimeSpan t = TimeSpan.ParseExact(time, "HH.mm.ssff", CultureInfo.InvariantCulture);

这里有什么问题?

首先,您使用 . 作为分隔符,但您的字符串使用 :.

其次,这是秒(基于 60 的数字)和毫秒(基于 100 的数字)的一种非常奇怪的表示,因此您更有可能:

string time = "12:25:11.97" // remember the quotes

应该用以下方法解析:

TimeSpan t = TimeSpan.ParseExact(time, "hh':'mm':'ss.ff", CultureInfo.InvariantCulture);

如果你确实有12:25:1197那么你可以使用hh':'mm':'ssff,但这确实很奇怪

顺便说一句,如果那是您所谓的 ms 的两位数,那么就是 hundreths of seconds,而不是 milliseconds(应该是三位数)

这个有效:

TimeSpan t = TimeSpan.ParseExact(time, "hh\:mm\:ssff", CultureInfo.InvariantCulture);

基于:https://msdn.microsoft.com/en-us/library/ee372287.aspx#Other