繁体   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#?

我想设置两个TimeSpans lasttimestamp和time并将它们设置为0,如下所示:

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

然后在以后的while循环中,我想将timestamp设置为hhmmss.fff格式的日志文件中的值,并从lasttimestamp时间跨度中减去它:

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

为何它不喜欢格式中的.fff部分,导致“输入字符串格式不正确”?

我尝试使用DateTime,但是执行减法时无法将TimeSpan转换为DateTime。

谢谢

你需要逃跑. 格式如@"hhmmss\\.fff"

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

但是,您应该使用TimeSpan.Zero设置零时间戳,例如:

TimeSpan lastTimeSpanZero = TimeSpan.Zero;

两者将返回相同的值。

(lasttimestamp == lastTimeSpanZero) == true

稍后,在您的解析中逃脱.

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

暂无
暂无

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

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