luxon
luxon copied to clipboard
toFormat function in persian and islamic calenders returns null for year value
Describe the bug
When I use a calendar other than gregorian
(in my case persian
and islamic
) and use toFormat
function, output will show null
for the year
value.
To Reproduce
import { DateTime, Settings } from "luxon"
const time = "2022-06-20T23:29:04.000000Z"
// gregory test
Settings.defaultLocale = "en"
Settings.defaultOutputCalendar = "gregory"
console.log(DateTime.fromISO(time).toFormat("dd MMMM y")) // outputs 21 June 2022
// persian test
Settings.defaultLocale = "fa"
Settings.defaultOutputCalendar = "persian"
console.log(DateTime.fromISO(time).toFormat("dd MMMM y")) // outputs ۳۱ خرداد null
// islamic test
Settings.defaultLocale = "ar"
Settings.defaultOutputCalendar = "islamic"
console.log(DateTime.fromISO(time).toFormat("dd MMMM y")) // outputs ٢٢ ذو القعدة null
Actual vs Expected behavior
Actual behavior: Outputs null
for year
value in islamic
and persian
but works ok with gregory
.
Expected behavior: year
values should not be null.
Desktop (please complete the following information):
- OS: windows 10, ubuntu 20.04 LTS
- Browser: Chrome 102
- Luxon version: ^2.4.0
- Your timezone: Asia/Tehran
Additional context
Using luxon
on a react
project.
By the way the following information might be useful: When I tried using DateTime.fromISO(time).toLocaleParts()
in gregory
calendar, The return value was something like [..., { year: 2022 }, ...]
. But when I tried toLocaleParts
in persian
or islamic
, the return value was something like [..., { yearName: 1401 }, ...]
. Which means that the year
value has different keys
Confusingly, I can't reproduce this. For Farsi + Persian calendar, I get ۳۰ خرداد ۱۴۰۱
. For Arabic + Islamic calendar, I get 21 ذو القعدة 1443
. I'm on a Mac though, tried with Chrome 102 and 103. I'll check later when I'm at a Windows computer
Which means that the year value has different keys
This would certainly explain it -- Luxon finds the year by looking up the year
key in the result of Intl.DateTimeFormat.formatToParts
. The Intl spec has grown a bit in this regard (there is also relatedYear
) and I haven't quite caught up on how they're used
Same thing happening for spanish
datepicker and daterangepicker bug in Arabic and Persian https://github.com/odoo/odoo/issues/103875