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.