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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

isToday(new UTCDate()) => false #3730

Closed
tilowestermann opened this issue Mar 13, 2024 · 4 comments 路 Fixed by #3731
Closed

isToday(new UTCDate()) => false #3730

tilowestermann opened this issue Mar 13, 2024 · 4 comments 路 Fixed by #3731

Comments

@tilowestermann
Copy link

There is an issue with the isToday function

The problem

馃捇 Code demonstrating the problem

import { UTCDate } from '@date-fns/utc';
import { isToday } from 'date-fns';

isToday(new UTCDate());
//=> false

Demo: https://stackblitz.com/edit/stackblitz-starters-bdluhl?file=src%2Fmain.ts

馃檨 Actual behavior

new UTCDate() is not today

馃檪 Expected behavior

new UTCDate() is today

Debug information

  • date-fns version: 3.4.0 (@date-fns/utc: 1.2.0)
  • Browser/Node.js version: Edge
  • Your timezone: UTC+1
  • Your current time: 15:00
@fturmel
Copy link
Member

fturmel commented Mar 13, 2024

date-fns is determining today with Date.now(). While this gives us the correct timestamp, it isn't wrapped in a UTCDate object. This causes the later startOfDay() calculation (used by isSameDay()) to fail because the start of the day isn't the same in UTC vs the local timezone.

There might be a need to audit all date-fns functions and use constructFrom() to create all "now" dates with a matching constructor when they are paired with a user-defined date. There are at least 8 functions affected by this interop problem.

A fix for isToday() could look like this:

// currently:
return isSameDay(date, Date.now()); 

// fix:
return isSameDay(date, constructFrom(date, Date.now()));

It would be a good idea to devise a testing strategy for this type of issue between the two projects.

@kossnocorp
Copy link
Member

@fturmel, you're right about that. Thank you for pointing the way. I will review it and ship a fix.

@fturmel fturmel linked a pull request Mar 14, 2024 that will close this issue
@kossnocorp
Copy link
Member

Fixed via b1a5eb5

@kossnocorp
Copy link
Member

Shipped with date-fns@3.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Shipped
Development

Successfully merging a pull request may close this issue.

3 participants