字符串格式数字的时差 [英] Time difference from string format numbers
本文介绍了字符串格式数字的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有以下两个字符串。
11:16:18 :304
11:17:32:00
我想以时间格式考虑这两种情况,并找出以毫秒为单位的差异。关于怎么做的任何评论?
我累了,
Hi all,
I have two strings as follows.
11:16:18:304
11:17:32:007
I want to consider those two in time format and find the difference in milliseconds. Any comments on how to do this?
I tired,
TimeSpan ts = TimeSpan.Parse("11:16:18:304");
然而第一个冒号返回错误并寻找一个点。
在此先感谢!
however the first colon returns an error and looking for a dot instead.
Thanks in advance!
推荐答案
string format = "HH:mm:ss:fff";
DateTime from = DateTime.ParseExact("11:16:18:304", format, System.Globalization.CultureInfo.InvariantCulture);
DateTime to = DateTime.ParseExact("11:17:32:007", format, System.Globalization.CultureInfo.InvariantCulture);
var totalmilliseconds = (to - from).TotalMilliseconds;
DateTime t1 = DateTime.ParseExact("11:16:18:304", "HH:mm:ss:fff", null);
DateTime t2 = DateTime.ParseExact("11:17:32:007", "HH:mm:ss:fff", null);
TimeSpan t = t2 - t1;
快乐编码!
:)
Happy Coding!
:)
传递给TimeSpan.Phrase的字符串的格式声明是[ws] [ - ] {d | [d。] hh:mm [:ss [.ff]]} [ws]
。
所以你的字符串格式错误应该是11:16:18。
304和11:17:32。
007!
The format declaration for the string passed to TimeSpan.Phrase is this[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
.
so your string in the wrong format it should be 11:16:18.
304 and 11:17:32.
007!
这篇关于字符串格式数字的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文