Formating Date in Freemarker to say "Today", "Yesterday", etc.
Posted
by
egervari
on Stack Overflow
See other posts from Stack Overflow
or by egervari
Published on 2010-12-07T20:16:50Z
Indexed on
2011/01/08
3:54 UTC
Read the original article
Hit count: 270
Is there a way in freemarker to compare dates to test if the date is today or yesterday... or do I have to write code in Java to do these tests?
I basically want to do this:
<#------------------------------------------------------------------------------
formatDate
------------------------------------------------------------------------------->
<#macro formatDate date showTime=true>
<#if date??>
<span class="Date">
<#if date?is_today>
Today
<#elseif date?is_yesterday>
Yesterday
<#else>
${date?date}
</#if>
</span>
<#if showTime>
<span class="Time">${date?time}</span>
</#if>
</#if>
</#macro>
EDIT: My best guess to implement this is to pass "today" and "yesterday" into the model for the pages that use this function and then compare the date values against these 2 objects in the model. I am out of out of options, but I'd rather not have to do this for every page that uses this macro. Any other options that are nicer?
<#if date??>
<span class="Date">
<#if date?date?string.short == today?date?string.short>
Today
<#elseif date?date?string.short == yesterday?date?string.short>
Yesterday
<#else>
${date?date}
</#if>
</span>
<#if showTime>
<span class="Time">${date?time}</span>
</#if>
</#if>
© Stack Overflow or respective owner