Compare datetimes ignoring time zone
Solution 1:
Try this:
var dt1 = DateTimeOffset.Parse("2022-01-17T18:59:43.0030684+06:00");
var dt2 = DateTimeOffset.Parse("2022-01-17T18:59:43.0030684+03:00");
Console.WriteLine(DateTime.Compare(dt1.DateTime, dt2.DateTime));
Solution 2:
Technically, you can use DateTimeOffset
instead of DateTime
in order to compensate (i.e. add) difference of time zones:
Code:
var dt1 = DateTimeOffset.Parse("2022-01-17T18:59:43.0030684+06:00");
var dt2 = DateTimeOffset.Parse("2022-01-17T18:59:43.0030684+03:00");
Console.WriteLine(DateTimeOffset.Compare(dt1 + dt1.Offset, dt2 + dt2.Offset));
Outcome: (fiddle)
0