总毫秒数 [英] Total number of millseconds
本文介绍了总毫秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿伙计们,我想找到两次之间的总毫秒数。我第一次进行转换以输入正确的格式,然后我继续进行计算。然而,我的结果如果非常小,我似乎无法找出什么是错的。
Hey guys i want to find the total number of milliseconds between 2 time. I do a conversion on the first time to put in the correct format, then i proceed to to the calculation. However my result if very small and i can't seem to find out whats wrong.
string dateValues = "10042011" ;
string pattern = "MMddyyyy";
DateTime parsedDate=new DateTime();
if (DateTime.TryParseExact(dateValues, pattern, null, DateTimeStyles.None, out parsedDate))
{
Console.WriteLine("Converted to this: " + parsedDate);
}
DateTime d1 = parsedDate;
DateTime d2 = new DateTime(1970, 1, 1);
TimeSpan ts = new TimeSpan(d1.Ticks - d2.Ticks);
Console.WriteLine("Converted to this: " + ts);
输出为:15982.00:00:00,我非常确定这不正确。
感谢您的帮助
the output is : 15982.00:00:00, and i am very sure this not correct.
thanks for your help
推荐答案
使用System.TimeSpan.TotalMilliseconds
:
http:// msdn .microsoft.com / zh-cn / library / system.timespan.totalmilliseconds.aspx [ ^ ]。
-SA
删除新东西 - 两个DateTime对象之间的差异已经是Timespan:
Drop the new stuff - the difference between two DateTime objects is a Timespan already:
TimeSpan ts = d1 - d2;
Console.WriteLine("Converted to this: " + ts.TotalMilliseconds);
你的代码看起来......好吧......如果你拿一个日期并从中减去一个日期,你会得到一个TimeSpan对象,不是约会。您不必创建新的Timespan。您的代码应该更接近:
Your code looks... well... If you take a date and subtract a date from it, you get a TimeSpan object back, not a date. You don't have to create a new Timespan. Your code should be closer to:
DateTime beginDate = new DateTime(1970, 1, 1);
DateTime endDate = DateTime.Now;
TimeSpan difference = endDate - beginDate;
Debug.WriteLine(difference.TotalMilliseconds);
这篇关于总毫秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文