这是将日期添加为类似时代前格式的最佳解决方案:
渲染流体部分如下:
<f:render section="timeAgo" arguments="{posted:'{posting.datePosted}'}" />
流体部分:
<f:section name="timeAgo">
<f:variable name="now" value="{f:format.date(date: 'now',format:'%s')}" />
<f:variable name="posted" value="{f:format.date(date:'{posted}',format:'%s')}" />
<f:variable name="diff" value="{now - posted}" />
<f:if condition="{diff} < 60">
<f:then>Now</f:then>
<f:else if="{diff} < 3600">
<f:format.number decimals="0">{diff / 60}</f:format.number> minute ago
</f:else>
<f:else if="{diff} < 86400">
<f:format.number decimals="0">{diff / 3600}</f:format.number> hours ago
</f:else>
<f:else if="{diff} < 604800">
<f:format.number decimals="0">{diff / 86400}</f:format.number> days ago
</f:else>
<f:else if="{diff} < 2419200">
<f:format.number decimals="0">{diff / 604800}</f:format.number> weeks ago
</f:else>
<f:else if="{diff} < 29030400">
<f:format.number decimals="0">{diff / 2419200}</f:format.number> months ago
</f:else>
<f:else>{f:format.date(date: '{posted}',format:'%d %B, %Y')}</f:else>
</f:if>
</f:section>
只需在 arguments="{posted:'{posting.datePosted}'}" 处传递正确的变量对象即可。
谢谢!