Skip to content

Instantly share code, notes, and snippets.

@planetsizebrain
Created April 11, 2017 10:06
Show Gist options
  • Save planetsizebrain/a9dc41e72f3fba2a1bd84caef0741ac5 to your computer and use it in GitHub Desktop.
Save planetsizebrain/a9dc41e72f3fba2a1bd84caef0741ac5 to your computer and use it in GitHub Desktop.
Liferay Freemarker datetime format with 4-digit year
<#setting time_zone=timeZone.ID>
<#setting locale=locale.toString()>
<#setting datetime_format="MM/dd/yyyy">
<ul>
<#list entries as entry>
<li><li>${entry.title} - ${entry.modifiedDate?datetime}</li></li>
</#list>
</ul>
@caneta
Copy link

caneta commented Apr 11, 2017

This snippet makes sense only if you have en_EN or en_US locale: this way english users will see dates in format MM/dd/yyyy, as they expect.

Here, if I change my portal language to italian and so my locale becomes it_IT, the datetime format remains MM/dd/yyyy because it is forced on that format at line 3.
But this is not the desired behaviour: if locale is it_IT, I should see a dd/MM/yyyy format instead.

Is there a way to solve this?

Thank you

@planetsizebrain
Copy link
Author

OK, now I understand what you want to achieve. In that case I think you might need to use conditional construction that sets the datetime_format according to the current portal locale.

<#setting time_zone=timeZone.ID>
<#setting locale=locale.toString()>

<#assign portalLocale=locale.toString()>
<#if portalLocale="en_US">
  <#setting datetime_format="MM/dd/yyyy">
<#else>
  <#setting datetime_format="dd/MM/yyyy">
</#if>

${.now?datetime}

@caneta
Copy link

caneta commented Apr 19, 2017

Ok, not very confortable if I have to manage a portal supporting 30 different languages, but I got the point.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment