FreeMarker Current Date Comparison

十年热恋 提交于 2019-12-05 17:52:33

问题


Is it possible to do date comparisons against the current date in a freemarker template without passing the current date into the template?


回答1:


From FreeMarker 2.3.17 on you can use the new special variable .now:

[#assign foo = .now > yesterday?datetime]



回答2:


In case someone else has this issue.

I was comparing 2 dates

[#assign .now?date lte today?date]

(they both were 10/10/2019)

but for some reason it was false even though it was equal, so what I had to do was

[#assign .now?string["dd/MM/yyyy"]?date lte today?date]



回答3:


Better way to compare two dates in freemarker is to find the difference between two dates.

<#assign difference = (custom_date?long / 86400000)?round - (.now?long / 86400000)?round />

If the difference is greater than 0, it's a future date, if it equal it is same date, if it less than, it is previous date




回答4:


NO

Freemarker is a templating library, there isn't a today variable.

But you can pass a new Date() Java Object to the template.



来源:https://stackoverflow.com/questions/1373941/freemarker-current-date-comparison

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!