Search code examples
freemarker

How to write a datetime format in FTL


I am trying to create a expiration date translator depending on locale

(Australia)

Actual Date: Ends 31 Jul, 2022 23:59:59 AEST.

Expected: Ends 31 Jul 2022

(France)

Actual Date: Expire le juil. 31, 2022

Expected: Expire le 31 juil. 2022

My thought, unsure how to code it, please help

<#-- The string that comes from somewhere: -->
<#assign ExpiredOn = '(Actual)'>

<#setting date_format="(Unsure how to format it whichi need help)">
<#setting locale="(My region to be used)">

or

<#if locale = "">
  //use these format//
<#elseif locale= ""
  //use these format?
</#if>

Solution

  • I cannot reproduce your issue. When I try:

    <#setting locale="fr">
    <#assign expiryDate = "2022-07-31"?date("yyyy-MM-dd")>
    Expire le ${expiryDate}
    

    I get Expire le 31 juil. 2022.

    Anyway, if you want a specific format, you can indeed use <#setting date_format="...">. Please refer to the date time format settings documentation for details. Patterns must be in Java's SimpleDateFormat.