简体   繁体   English

TimeSpan.ParseExact(“ 000000.000”,“ hhmmss.fff”,CultureInfo.CurrentCulture); 输入的字符串格式不正确C#?

[英]TimeSpan.ParseExact(“000000.000”, “hhmmss.fff”, CultureInfo.CurrentCulture); Input string was not in a correct format C#?

I want to setup two TimeSpans lasttimestamp and time and set them to 0 like this : 我想设置两个TimeSpans lasttimestamp和time并将它们设置为0,如下所示:

        TimeSpan lasttimestamp = TimeSpan.ParseExact("000000.000","hhmmss.fff",CultureInfo.CurrentCulture);
        TimeSpan time = TimeSpan.ParseExact("000000.000", "hhmmss.fff", CultureInfo.CurrentCulture);   

then in a later while loop i want to set timestamp to a value in a log file in in the format hhmmss.fff and subtract it from the lasttimestamp timespan : 然后在以后的while循环中,我想将timestamp设置为hhmmss.fff格式的日志文件中的值,并从lasttimestamp时间跨度中减去它:

TimeSpan timestamp = TimeSpan.ParseExact(splitline[1], "hhmmss.fff", CultureInfo.CurrentCulture);
 time = timestamp.Subtract(lasttimestamp);

how ever it does not like the .fff part in the formating resulting in "Input string was not in a correct format" ? 为何它不喜欢格式中的.fff部分,导致“输入字符串格式不正确”?

I have tried with DateTime but get cannot convert TimeSpan to DateTime when performing the subtraction. 我尝试使用DateTime,但是执行减法时无法将TimeSpan转换为DateTime。

Thanks 谢谢

You need to escape . 你需要逃跑. in your format like @"hhmmss\\.fff" : 格式如@"hhmmss\\.fff"

 TimeSpan lasttimestamp = TimeSpan.ParseExact(@"000000.000", 
                            @"hhmmss\.fff", CultureInfo.CurrentCulture);

But, you should use TimeSpan.Zero to set up zero time stamp like: 但是,您应该使用TimeSpan.Zero设置零时间戳,例如:

TimeSpan lastTimeSpanZero = TimeSpan.Zero;

Both will return same value. 两者将返回相同的值。

(lasttimestamp == lastTimeSpanZero) == true

Later on, in your parsing escape the . 稍后,在您的解析中逃脱. .

TimeSpan timestamp = TimeSpan.ParseExact(splitline[1], 
                          @"hhmmss\.fff", CultureInfo.CurrentCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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