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

Set date from small to large time unit #32

Merged
merged 1 commit into from
Jun 2, 2024

Conversation

ofekd
Copy link

@ofekd ofekd commented May 31, 2024

This avoids ambiguous situations where the desired month does not have as many days at the current month, leading to unexpected behavior.

For example, intending to create a date in June on the 31st of May, will result in the date being in July instead:

const date = new Date(); // May 31st, 2024
date.setUTCFullYear(2024); // Still May 31st, 2024
date.setUTCMonth(5); // No such thing as June 31st, sets to July 1st

This avoids ambiguous situations where the desired month does not have a
many days as the current date, leading to unexpected behavior.

For example, intending to create a date in June on the 31st of May, will
result in the date being in July instead:

```javascript
const date = new Date(); // May 31st, 2024
date.setUTCFullYear(2024); // Still May 31st, 2024
date.setUTCMonth(5); // No such thing as June 31st, sets to July 1st
```
@ofekd
Copy link
Author

ofekd commented Jun 1, 2024

My first solution foolishly had the opposite problem. I've pushed an updated version.

@massivefermion
Copy link
Owner

massivefermion commented Jun 2, 2024

I understand the issue you mention but I still don't see how your PR fixes it. Whether you give the numbers separately or together, the behavior is the same, isn't it?

Also, could you explain how does this manifest itself when used from Gleam? Could you provide an example in Gleam that has this problem?

@ofekd
Copy link
Author

ofekd commented Jun 2, 2024

The problem is not setting an impossible date but having an impossible date carried over from today's date.

If today is May 31st, and I try to get a new date object to be June 1st, this will fail:

const date = new Date(); // May 31st, 2024, today's date
date.setUTCFullYear(2024); // Still May 31st, 2024
date.setUTCMonth(5); // No such thing as June 31st, sets to July 1st
date.setUTCDate(1); // July 1st, not what we were looking to get

This will work:

const date = new Date(Date.UTC(2024, 5, 1)); // June 1st

Edit:

A Gleam example would be, but only on JS target:

// With birl 1.7, If you run this on May 31st, this will parse as July 1st, not June 1st
birl.parse("2024-06-01T00:00:00.000Z")

@massivefermion
Copy link
Owner

yeah, makes sense.
Thanks for the PR.

@massivefermion massivefermion merged commit 17b48be into massivefermion:main Jun 2, 2024
1 check passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants