Converting Dates Freemarker

2019-09-12 00:43发布

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条回答
时光不老,我们不散
2楼-- · 2019-09-12 01:28

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).

查看更多
登录 后发表回答