moment / luxon

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

`expandFormat` does not escape non token strings #1511

Open alexfauquette opened 10 months ago

alexfauquette commented 10 months ago

Hi, we have a bug on @mui/x-pickers when using the expandFormat with Brazilian locale https://github.com/mui/mui-x/issues/10177

Basically, formatting a date with the extension of the token DD is buggy. since it contains de word, which is not escaped, leading to the d being transformed in the day number.

To Reproduce

Codesandbox reproduction:

const date = DateTime.fromISO("2014-08-06T13:00:00.000Z");
const format = DateTime.expandFormat("DD", { locale: "pt-BR" }); // d de MMM de yyyyy

// Broken formating
const withExpandFormat = date.toFormat(format, { locale: "pt-BR" });

// Working formating
const withoutExpandFormat = date.toFormat("DD", { locale: "pt-BR" });

withExpandFormat ("d de MMM de yyyyy"): 6 6e ago. 6e yyyyy withoutExpandFormat ("DD"): 6 de ago. de 2014

Actual vs Expected behavior

I would expect date.toFormat(format, { locale: "pt-BR" }); to escape non token string as follow

- d de MMM de yyyyy
+ d 'de' MMM 'de' yyyyy

Config