Converting Dates Freemarker

半城伤御伤魂 提交于 2019-12-12 02:34:20

问题


I'm trying to get the following to work but I can't get the correct value to display.

Assuming SHIPPING_DATE is treated as a string with the value = 2016/05/23:

<#setting date_format="MM/dd/yyyy">
<#setting locale="en_US">

<#assign ship_date>${SHIPPING_DATE}</#assign>

${ship_date?date("MM/dd/yyyy")}

The output is 12/05/0190 but I'm expecting 05/23/2016. Will someone help but also explain what I'm doing wrong, please?


回答1:


If you have a string in ship_date like 2016/05/23, then you can parse it to a real date value with ship_date?date("yyyy/MM/dd"). Note that it's not MM/dd/yyyy, as in your example. ?date means "convert to date", and you specify to it how to interpret the string. Then, when you print a real date value (not a string) with ${...}, then it will be converted to string according the data_format configuration setting, so then MM/dd/yyyy will be good. It doesn't mater how that real date value was get (like with string?date(format) or directly from the data-model).



来源:https://stackoverflow.com/questions/38235637/converting-dates-freemarker

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