Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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)}}
share|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() }
share|improve this answer
    
How do you set the preferred format for the locale? –  Marion Newlevant Jul 15 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 at 8:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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