---
title: "freemarker ?datetime ? time ?date"
author: "ych0108"
date: "2014-04-30T16:28:27Z"
tags: ["HTML", "freemarker"]
categories: ["Web前端"]
slug: "freemarker-datetime-time-date"
draft: false
lastmod: "2014-04-30T16:28:27Z"
id: 2058932
url: "https://ych0108.iteye.com/blog/2058932"
---

?date,?time和?datetime，因为你指定的格式告诉FreeMarKer显示日期的哪 部分。
无论如何，FreeMarker都会相信你，so you can show "noise" if you display parts that are actually not stored in the variable.例如：${openingTime?string("yyyy-mm-dd hh:mm:ss a")},openingTime只存储了时间。将会显示1790-01-01 09:24:44 PM.
格式也可以是short,medium……"short_medium"等等。这样跟你用"."使用预定义的格式是一样的:someDate?string("short"）和someDate?string.short是相当的。
date,time,datetime
这些标签可以用来指定日期变量中的哪些部分被使用。
date:只使用年、月、日
time:只使用时、分、秒和毫秒部分
datetime:日期和时间两部分都被使用
理想情况下，你不需要使用它们。不幸的是，由于java平台的技术限制。FreeMarker有的时候不能找到日期变量使用的部分（例如：只有年月 日，或者只有时分秒，或者两者）询问程序员那个变量存在这个问题。如果FreeMarker需要执行一个需要这个变量的操作－－就像把日期作为字符显示 －－但是它不知道使用那些部分，它会停下来报错。这就是你必须使用这些标签的情况。例如：假定openingTime就是这样一个问题变量：

```html
<#assign x = openingTime> <#-- no problem can occur here -->
${openingTime?time} <#-- without ?time it would fail -->
<#-- For the sake of better understanding, consider this: -->
<#assign openingTime = openingTime?time>
${openingTime} <#-- this will work now -->

另一种用法：切短日期。例如：
Last updated: ${lastUpdated} <#-- assume that lastUpdated is a date-time value -->
Last updated date: ${lastUpdated?date}
Last updated time: ${lastUpdated?time}

将显示：
Last updated: 04/25/2003 08:00:54 PM
Last updated date: 04/25/2003
Last updated time: 08:00:54 PM
```