Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hamzanz opened this issue Feb 21, 2025 · 0 comments

Comments

@hamzanz
Copy link

hamzanz commented Feb 21, 2025

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:

// 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:

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant