Skip to content

DateTime Comparison fails with different DateTimeKind #88309

@AdamRadicsTalison

Description

@AdamRadicsTalison

Description

When comparing UTC vs local time, it seems the offset is not applied before comparison.

Reproduction Steps

I am in the UTC+8 time zone.

// 9.20am
DateTime laterUTC = new DateTime(2023, 06, 26, 01, 20, 00, DateTimeKind.Utc);

// 08:50am
DateTime earlierLocal = new DateTime(2023, 06, 26, 08, 50, 00, DateTimeKind.Local);

laterUTC.ToString("O").Dump();
earlierLocal.ToString("O").Dump();

($"Later UTC < Earlier Local: {(laterUTC < earlierLocal)}").Dump();

($"Later UTC < Earlier UTC: {(laterUTC < earlierLocal.ToUniversalTime())}").Dump();

($"Later Local < Earlier Local: {(laterUTC.ToLocalTime() < earlierLocal)}").Dump();

Expected behavior

2023-06-26T01:20:00.0000000Z
2023-06-26T08:50:00.0000000+08:00
Later UTC < Earlier Local: False
Later UTC < Earlier UTC: False
Later Local < Earlier Local: False

Actual behavior

2023-06-26T01:20:00.0000000Z
2023-06-26T08:50:00.0000000+08:00
Later UTC < Earlier Local: True
Later UTC < Earlier UTC: False
Later Local < Earlier Local: False

Regression?

No response

Known Workarounds

Convert kind before comparison.

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions