问题
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