iamkun / iamkun/dayjs

`isSameOrAfter()` returns incorrect results when used with `tz('UTC', true)` and time comparison

Open
#2,829 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
Description:
When comparing times using isSameOrAfter() in combination with tz('UTC', true), two issues occur:

tz('UTC', true) with keepLocalTime shifts the displayed times despite the keepLocalTime: true parameter
isSameOrAfter() returns incorrect comparison results when used with these timezone-converted times

**Steps to Reproduce:**

```typescript
// Setup
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(isSameOrAfter);

// Test function
function compareTimeTest(targetTime: string, referenceTime: string) {
const targetDateTime = dayjs(`1970-01-01 ${targetTime}`).tz('UTC', true);
const referenceDateTime = dayjs(`1970-01-01 ${referenceTime}`).tz('UTC', true);

console.log('Input times:', { targetTime, referenceTime });
console.log('Dayjs string representation:', {
target: targetDateTime.toString(),
reference: referenceDateTime.toString()
});
console.log('Formatted times:', {
targetTime: targetDateTime.format('HH:mm:ss'),
referenceTime: referenceDateTime.format('HH:mm:ss')
});
console.log('Timestamp values:', {
targetValue: targetDateTime.valueOf(),
referenceValue: referenceDateTime.valueOf()
});
console.log('Comparisons:', {
valueComparison: targetDateTime.valueOf() >= referenceDateTime.valueOf(),
isSameOrAfter: targetDateTime.isSameOrAfter(referenceDateTime)
});
}

// Run test
compareTimeTest('07:00:00', '08:00:00');
```

**Current Output:**
```shell
Input times: {
targetTime: '07:00:00',
referenceTime: '08:00:00'
}
Dayjs string representation: {
target: 'Thu, 01 Jan 1970 07:00:00 GMT',
reference: 'Thu, 01 Jan 1970 08:00:00 GMT'
}
Formatted times: {
targetTime: '12:00:00',
referenceTime: '13:00:00'
}
Timestamp values: {
targetValue: 25200000,
referenceValue: 28800000
}
Comparisons: {
valueComparison: false,
isSameOrAfter: true
}
```

**Expected behavior**

With keepLocalTime: true, the times should remain as 07:00:00 and 08:00:00 without timezone shifting
isSameOrAfter() should return false since 07:00:00 is not after 08:00:00

**Actual Behavior:**

Times are shifted by timezone offset (showing as 12:00:00 and 13:00:00 in EST timezone)
isSameOrAfter() returns true incorrectly, while direct timestamp comparison (valueOf()) correctly returns false

**Information**
- Day.js Version v1.11.10
- OS: MacOS
- NodeJS
- Time zone: EST (UTC-5)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.