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

startOfMonth returning wrong date when input is first day of the month #3761

Open
AdamMarciniak opened this issue Apr 10, 2024 · 1 comment

Comments

@AdamMarciniak
Copy link

When I run this:

console.log(new Date('2021-01-01'))
console.log('START', startOfMonth(new Date('2021-01-01')))

I get:

2021-01-01T00:00:00.000Z
START 2020-12-01T08:00:00.000Z

The startOfMonth function goes backwards to the previous month.

If I specify the 2nd of the month it works how it should.

@sumodmatto
Copy link

@AdamMarciniak

I couldn't reproduce this issue in my environment, but I believe it is similar to the problem described in issue #3764. It seems that the date gets processed as a local timezone within date-fns, which could be causing the discrepancy.

As mentioned in #3764, using UTCDate should prevent this issue:
UTCDate companion package

import { startOfMonth } from "date-fns";
import { UTCDate } from "@date-fns/utc";

console.log(new UTCDate("2021-01-01"));
console.log("START", startOfMonth(new UTCDate("2021-01-01")));
console.log("START", startOfMonth(new UTCDate(2021, 0, 1)));

By using UTCDate, you should be able to get consistent results as expected.

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

2 participants