Twig: format_datetime doesn't work right with date_modify

Created on 19 Mar 2020  路  1Comment  路  Source: twigphp/Twig

Hi,

I've tried to do the following

{{ '2020-01-01' | date_modify('-1 day') | format_datetime(pattern='dd MMMM Y', locale='en') }}

and its output is 31 December 2020 while I was expecting it to give me the year 2019, as the following code does

{{  '2020-01-01' | date_modify('-1 day') | date('d F Y') }}

For the records, it seems to be linked to the year passed. For instance, with 2017-01-01, the output will be correct.

Most helpful comment

That's because Y means "Year of the week" in intl formaters, that's why you get 2020 because the 31th of december is part of the first week of 2020. You get different result depending of the year because depending of the year it's either part of the last week of the year or part of the first week of the next year.

To get the year of the date you need to use y instead (https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classSimpleDateFormat.html#details).

>All comments

That's because Y means "Year of the week" in intl formaters, that's why you get 2020 because the 31th of december is part of the first week of 2020. You get different result depending of the year because depending of the year it's either part of the last week of the year or part of the first week of the next year.

To get the year of the date you need to use y instead (https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classSimpleDateFormat.html#details).

Was this page helpful?
0 / 5 - 0 ratings