0
votes

I'm trying to compare 2 datetimeoffset with 2 different time zones with mongodb c# driver. I'm using the document serialization for datetimeoffset that create an object that include: - datetime in UTC - ticks in LOCAL - offset

I want to compare only the datetime part of the object because "2019-05-03 10:00:00 +01" are equal to "2019-05-03 09:00:00 +00".

Thanks

1

1 Answers

0
votes

There are a number of methods that can compare DateTimeOffset. The methods compare the UTC value of the DateTimeOffset

Compare returns an int:

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 similar to compare (different syntax), returns an 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 returns boolean:

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 compares the offset as well as the time, and returns a bool:

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

See this fiddle.