如何使用MongoDB C#驱动程序使用UTC日期时间比较两个datetimeoffset

时间:2019-05-03 08:31:12

标签: c# mongodb serialization datetimeoffset

我正在尝试使用mongodb c#驱动程序将2 datetimeoffset与2个不同时区进行比较。 我使用datetimeoffset的文档序列化创建了一个对象,该对象包括: -UTC中的日期时间 -在本地打勾 -偏移量

我只想比较对象的日期时间部分,因为“ 2019-05-03 10:00:00 +01”等于“ 2019-05-03 09:00:00 +00”。

谢谢

1 个答案:

答案 0 :(得分:0)

有许多方法可以比较DateTimeOffset。这些方法比较DateTimeOffset

的UTC值

Compare返回一个整数:

var c = DateTimeOffset.Compare(dto1, dto2);
// c > 0: dto1 is later
// c < 0: dto2 is later
// c == 0: dto1 and dto2 are the same in UTC

CompareTo类似于compare(不同的语法),返回一个int:

var c = dto1.CompareTo(dto2);
// c > 0: dto1 is later
// c < 0: dto2 is later
// c == 0: dto1 and dto2 are the same in UTC

Equals返回布尔值:

var c = dto1.Equals(dto2);
// True: dto1 and dto2 have the same value in UTC
// False: dto1 and dto2 do not have the same UTC value

EqualsExact比较偏移量和时间,并返回布尔值:

var c = dto1.EqualsExact(dto2);
// True: dto1 and dto2 have the same value in UTC AND the same Offset
// False: dto1 and dto2 do not have the same UTC value or do not have the same Offset

请参阅此fiddle