简体   繁体   中英

How can I parse a TimeSpan with milliseconds included?

I have an application that reads 'timed' data out of a file; currently, my input is something like this:

-- EDIT -- I have found an actual case where this fails.

0:0:1:934 > >> GOOD MORNING.<br>

But I seem to be having some trouble parsing this into a valid TimeSpan. I would appreciate if someone could point me in the right direction, as not many offerings that I've found thus far provided much in the way of solving the problem.

As my code stands;

String StoredTime = ArchiveLine.Split('>')[0].TrimEnd();
String StoredFrame = ArchiveLine.Substring(ArchiveLine.IndexOf('>')+1).TrimStart();

TimeSpan FrameTime = TimeSpan.Parse(StoredTime, DateTimeFormatInfo.InvariantInfo);

And it throws a format exception.

Thanks.

It may be a localisation issue. Some cultures use a comma instead of a period as the decimal point. Try:

TimeSpan FrameTime = TimeSpan.Parse(StoredTime, DateTimeFormatInfo.InvariantInfo); 

这里的问题是(I)在Parse方法中引用了我行的错误部分...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM