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

format_date: Dates in the last 2 days of 2024 get misformatted as dates in 2025 when using a custom format #1177

Open
akx opened this issue Jan 23, 2025 · 4 comments

Comments

@akx
Copy link
Member

akx commented Jan 23, 2025

>>> import babel.dates
>>> babel.dates.format_date(datetime.date(2024, 12, 30), format="YYYY-MM-dd (cccc)", locale="en")
'2025-12-30 (Monday)'
>>> babel.dates.format_date(datetime.date(2024, 12, 29), format="YYYY-MM-dd (cccc)", locale="en")
'2024-12-29 (Sunday)'
>>> babel.dates.format_date(datetime.date(2024, 12, 29), format="long", locale="en")
'December 29, 2024'
>>> babel.dates.format_date(datetime.date(2024, 12, 30), format="long", locale="en")
'December 30, 2024'
>>> babel.dates.format_date(datetime.date(2024, 12, 31), format="long", locale="en")
'December 31, 2024'
>>> babel.dates.format_date(datetime.date(2024, 12, 30), format="YYYY-MM-dd (cccc)", locale="en")
'2025-12-30 (Monday)'
>>> babel.dates.format_date(datetime.date(2024, 12, 30), format="YYYY-MM-dd (cccc)", locale="fi")
'2025-12-30 (maanantai)'
>>> babel.dates.format_date(datetime.date(2024, 12, 30), format="short", locale="fi")
'30.12.2024'
>>> babel.dates.format_date(datetime.date(2024, 12, 29), format="short", locale="fi")
'29.12.2024'

Related to #1171.

Note long/short etc. isn't affected, just the custom formats.

@Esterbi
Copy link

Esterbi commented Jan 24, 2025

Also same problem with last 3 days in 2025 if it can help somehow (but not with any last days in 2023 or 2022) I've closed my issue about that by my mistake, so don't rely on it. Thanks for checking and reopenning!

@Esterbi
Copy link

Esterbi commented Jan 24, 2025

@akx DeepSeek has answer

The error in the python/babel library is related to the incorrect use of the YYYY format in the format string "YYYY-MM-dd (cccc)". In the Babel library (and other date-handling libraries like Moment.js), YYYY represents the week year, not the calendar year. The week year can differ from the calendar year, especially in the last days of December or the first days of January, if the week transitions into the next year.
Why does this happen?

Week year (YYYY) is determined by ISO weeks. If December 30 or 31 fall into the first week of the next year, YYYY will return the next year.

Calendar year (yyyy) always returns the current year, regardless of the week.

How to fix it?

Use yyyy instead of YYYY in the format string to get the correct calendar year:

Examples:

YYYY-MM-dd (cccc) → 2025-12-30 (Monday) (incorrect, uses the week year)

yyyy-MM-dd (cccc) → 2024-12-30 (Monday) (correct, uses the calendar year)

Why does it work with format="long" and format="short"?

When using built-in formats like "long" or "short", the Babel library automatically selects the correct year format (yyyy), so the issue does not occur.
Conclusion:

The error is caused by using YYYY instead of yyyy. To correctly display the calendar year, always use yyyy in custom formats.

@tomasr8
Copy link
Member

tomasr8 commented Jan 24, 2025

Thanks for the investigation! So I take it there's nothing to fix on babel's side? FYI, the date patterns are documented here: https://babel.pocoo.org/en/latest/dates.html#pattern-syntax

@akx
Copy link
Member Author

akx commented Jan 25, 2025

I do wonder if #1133 (#1146) is related, though. Might not be...

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

No branches or pull requests

3 participants