cjdinger
10 years agoLeader
Creating a Day countdown in Freemarker
Just sharing this because I needed one for an event group that we've created. We wanted a "countdown" to the event to appear on the page, but of course we're not interested in having a person update the count each day!
Example display:
Here's the code I'm using within a Custom Content Module. In this case, our event begins on Apr 18, 2016.
<#assign now = .now?long> <#assign count = ( ( "2016-04-18T00:00:00+0000"?datetime("yyyy-MM-dd'T'HH:mm:ssZ")?long - now ) / (1000 * 60 * 60 * 24) + 1 )?int> <#-- divides milliseconds*seconds*hours-in-a-day, then adds 1 to count today --> <#if (count<=0)> <#assign count="ZERO"> </#if> <div style="text-align:center; font-size:18pt; color:#333"><b>${count} days to go!</b></div>
Of course it could be fancier with alternate messages as we get closer, then during the event, then post-event. (I'm a big advocate of "good enough" solutions, especially if it allows us to open a community space sooner!)