Skip to content

Instantly share code, notes, and snippets.

@horelvis
Forked from emiloberg/real-time-date-stamp.ftl
Created July 24, 2016 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horelvis/e78ec55bc5af1d915b1028d8219ff931 to your computer and use it in GitHub Desktop.
Save horelvis/e78ec55bc5af1d915b1028d8219ff931 to your computer and use it in GitHub Desktop.
This is how you get a real date/time-stamp (such as article published date) in Freemarker in Liferay. With support for multilingual sites. When working with multilingual/non-English sites, date/time-stamp can be a bit of a headache as the date strings are strings with english words in them, such as Thu, 08 May 2014 11:48:00 +0000, rather than a …
<#-- ----------------------------------------------------------------- ->
-
- GETTING REAL TIME/DATE-STAMP
-
- When we ask Liferay for the create date of an article (or really we
- ask Liferay for the 'display-date' which is settable by the user) we get
- a string looking like this: 'Thu, 08 May 2014 11:48:00 +0000'. This
- string will always be in english and when we want to create a datetime
- object from it, we need to parse it with english locale.
-
- Therefor, when working with a non-english site we need to:
- 1) Save the original locale (e.g. 'sv_SE') to a variable
- 2) Set locale to 'en_US'
- 3) Parse the date string and create a date object from it
- 4) Set locale back to original locale, from variable.
- 5) Create a date string from the date object.
-
- Common pitfall:
- Swedish example: Most dates have similar names in Swedish and English.
- e.g.: the English dates '08 Jan 2014', '08 Feb 2014', '08 Mar 2014',
- '08 Apr 2014' are all the same as their Swedish counterpart,
- and are therefor parseable without doing any magic.
-
- However '08 May 2014' is not the same in Swedish ('08 maj 2014'),
- neither is 'oct'/'okt'.
-
- When developing, make sure you're working with content with
- display-date may or october.
-
<- ------------------------------------------------------------------ -->
<#-- Get Display Date (not created-date which is equal to when a user last
modified the content)
-->
<#assign date = .vars['reserved-article-display-date'].data>
<#-- Load timezone from language files, as we have multiple timezones.
If we just had one timezone, set it like:
<#setting time_zone = "Europe/Stockholm">
-->
<#setting time_zone = languageUtil.get(locale, "template-timezone")>
<#-- Saving original locale to be able to reset it to that later -->
<#assign originalLocale = locale>
<#-- Set locale to en_US to be able to parse the date string and make it a date object -->
<#setting locale = 'en_US'>
<#assign date = date?datetime("EEE, d MMM yyyy HH:mm:ss Z")>
<#-- Set locale to the real (original) locale -->
<#setting locale = originalLocale>
<#-- As we want to display the date in different formats depending on the
language we get the date/time-format from the language files. Else
we could set it like this:
<#assign dateTimeFormat = "d MMMM yyyy">
-->
<#assign dateTimeFormat = languageUtil.get(locale, "template-datetime-format")>
<#-- Create a date string from the date object -->
<#assign date = date?string(dateTimeFormat)>
<#-- END GETTING REAL TIME/DATE-STAMP -->
<#-- ----------------------------------------------------------------- -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment