简体   繁体   English

比较日期时间与毫秒精度

[英]Compare datetime with millisecond precision

I'm trying to compare 2 datetimes in asp.net but its ignoring the millisecond part. 我正在尝试比较asp.net中的2个日期时间,但忽略了毫秒部分。

I tried use linq: 我尝试使用linq:

messages.OrderBy(x => x.Date);

and also tried 并尝试了

messages.OrderBy(x => x.Date).ThenBy(x=>x.Date.Millisecond);

and also using sort 并使用排序

messages.Sort((x, y) => DateTime.Compare(x.Date, y.Date));

and tried convert the datetime with string format but its also ignoring the milliseconds. 并尝试将日期时间转换为字符串格式,但也忽略了毫秒。

The datetime field in the object is bringing the datetime with the milliseconds correctly. 对象中的datetime字段正确地将日期时间带入了毫秒。 I'm using Asp.net MVC3 with databases Informix, Oracle and SQL Server. 我将Asp.net MVC3与Informix,Oracle和SQL Server数据库一起使用。

You've made a mistake somewhere, a DateTime is stored internally as a number 您在某个地方犯了一个错误, DateTime在内部存储为数字

Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 AD (CE) in the GregorianCalendar calendar ( source ) 时间值以100纳秒为单位进行测量,称为“滴答”,特定的日期是GregorianCalendar日历中从0001 AD(CE)午夜12:00(CE)开始的滴答数( 来源

When you sort using a DateTime , it is simply doing an integer sort using this underlying value. 当使用DateTime排序时,它只是使用此基础值进行整数排序。 Therefore if your DateTime instance has information about the number of milliseconds, it will be included in the sort. 因此,如果您的DateTime实例具有有关毫秒数的信息,它将包含在排序中。 This can be demonstrated using code such as: 可以使用以下代码来证明这一点:

var dates = new[]{
    new DateTime(2013,1,31,12,0,0,10),
        new DateTime(2013,1,31,12,0,0,20),
        new DateTime(2013,1,31,12,0,0,5)
};

foreach(var date in dates)
{
    Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss.fff}", date);
}

Console.WriteLine("-------------");

foreach(var date in dates.OrderBy(dt => dt))
{
    Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss.fff}", date);
}

Which gives the output: 给出输出:

2013-01-31 12:00:00.010
2013-01-31 12:00:00.020
2013-01-31 12:00:00.005
-------------
2013-01-31 12:00:00.005
2013-01-31 12:00:00.010
2013-01-31 12:00:00.020

Clearly demonstrating that sorting a list of datetimes correctly places the earlier number of milliseconds first. 清楚地表明,对日期时间列表进行正确排序会首先放置较早的毫秒数。

Try it for yourself: http://rextester.com/HYQIM13679 自己尝试一下: http : //rextester.com/HYQIM13679

As to why this isnt happening for you, thats impossible to answer as you've not supplied details of how you come by your list of objects containing a field with a DateTime which you are sorting on. 至于为什么这种情况不会发生在您身上,这是无法回答的,因为您没有通过包含要排序的带有DateTime的字段的对象列表来提供详细信息。 One possibility is that your source data is actually a string and you are using some variant of DateTime.Parse / DateTime.ParseExact and have forgotton to specify that you wish to capture the millisecond part so they are being set to zero for every instance. 一种可能是您的源数据实际上是一个字符串,并且您使用的是DateTime.Parse / DateTime.ParseExact某种变体,并且忘记了指定要捕获毫秒部分,因此将每个实例的毫秒部分设置为零。

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

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