如果我编写一个简单的方法来返回纪元时间和 之间的毫秒数DateTime.UtcNow
,我会得到一个正确的答案。但是,如果我编写一个方法来返回某个任意日期和纪元时间之间的毫秒数,则最后三位数字始终为零。“某个任意日期”意味着我将 DateTime.Parse("arbitrary date string") 的输出传递给该方法。据我所知,.Parse 返回的 DateTime 对象并未返回所有有效数字。
测试方法:
static void GetMillis()
{
DateTime dUtc = DateTime.UtcNow;
DateTime epoch = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc);
double utcmillis = (dUtc - epoch).TotalMilliseconds;
String timestamp = dUtc.ToString();
DateTime arbitrary = (DateTime.Parse(timestamp));
Console.WriteLine("Milliseconds between DateTime.UtcNow {0} \nand epoch time {1} are {2}", dUtc, epoch, utcmillis);
Console.WriteLine("Milliseconds between arbitrary date {0} \nand epoch time {1} are {2}", arbitrary, epoch, (arbitrary - epoch).TotalMilliseconds);
}
输出:
C:\src\vs\epochConverter\epochConverter\bin\Debug
{powem} [54] --> .\epochConverter.exe -debug
Milliseconds between DateTime.UtcNow 8/26/2012 11:12:31 PM
and epoch time 1/1/1970 12:00:00 AM are 1346022751385.8
Milliseconds between arbitrary date 8/26/2012 11:12:31 PM
and epoch time 1/1/1970 12:00:00 AM are 1346022751000
我不知道我是在做一些奇怪的错误还是不理解这里的数学。我在 MSDN 中进行了研究,但找不到与此差异相关的任何内容。我真的很希望能够按照描述计算毫秒——这可能吗?
谢谢。
mp