I have a multilingual site, and I want my dates formatted like:

  • English site: July 13, 2014
  • Italian site: 13 lulgio 2014

So the month name is translated, and the order of the elements changes as well.


Craft has extended Twig's date filter so that it translates the output (no documentation for this, but the code is in craft/app/etc/templating/twigextensions/CraftTwigExtension.php around line 152). So {{now|date('F j, Y')}} will give July 13, 2014 on the English site, and luglio 13, 2014 on the Italian site. Which is half way there. In order to get the Italian date to use the Italian format (j F Y), we need a translation for the format string. Add this to craft/translations/it.php. (For more info see Static Translations):

'F j, Y' => 'j F Y',

To output the translated date:

{{now|date('F j, Y'|t)}}
| improve this answer | |

If you don't need to customized the output you can also use the Crafts datetime methods localeDate() and localeTime() to return a date / time in the current locale's preferred date format.

{# Returns '7/15/2014' if current locale is 'en' #}
{# Returns '15.07.2014' if current locale is 'it' #}
{{ now.localeDate() }
| improve this answer | |
  • How do you set the preferred format for the locale? – Marion Newlevant Jul 15 '14 at 3:58
  • I don't think you can, @Marion. That's why your approach is better if you have a need to customize the output. – carlcs Jul 15 '14 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.