moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.23k stars 730 forks source link

Can't parse "Tue Jan 24 2023 23:59:39 GMT+0100 (Central European Standard Time)" #1445

Closed Kydlaw closed 1 year ago

Kydlaw commented 1 year ago

Describe the bug

Parsing of the string "Tue Jan 24 2023 23:59:39 GMT+0100 (Central European Standard Time)" using "EEE MMM dd yyyy HH:mm:ss 'GMT'ZZ (ZZZZZ)" fails

To Reproduce

const dateString = 'Tue Jan 24 2023 23:59:39 GMT+0100 (Central European Standard Time)'
const formatString = "EEE MMM dd yyyy HH:mm:ss 'GMT'ZZ (ZZZZZ)"

// Parse the date string using the Luxon format
const dateTime = DateTime.fromFormat(dateString, formatString)

// Format the date in the desired format
const formattedDate = dateTime.toFormat('dd-MM-yyyy')

// Output the formatted date
console.log(formattedDate)

Actual vs Expected behavior Currently it returns an invalid DateTime I expected the .fromFormat() function to return a DateTime object containing the information from the string.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

diesieben07 commented 1 year ago

As documented in the table of tokens, ZZZZZ is not a valid parsing token. Luxon can only parse time zones from their IANA names (or from an offset, like you are already doing), this uses the token z. The reason for this is that there is no browser API to parse "human readable" time zone names and those names are not even consistent or unique.

icambron commented 1 year ago

This specific limitation is also discussed here: https://moment.github.io/luxon/#/parsing?id=limitations

paul-uz commented 1 year ago

Is there a viable workaround for this? I'm dealing with 3rd party data that uses the abbreviated timezone eg BST

diesieben07 commented 1 year ago

Unfortunately the only real option you have is to ship your own mapping of abbreviated zones to proper time zone names and do the replacement manually before passing it in to Luxon or otherwise. I'm not 100% sure where you'd find this, but most likely either in the IANA Time Zone Database or maybe in Unicode's ICU.