'获取30天前'和'一年前'的日期在freemarker中

时间:2015-07-09 04:59:52

标签: freemarker

我使用以下代码获取当前日期:

<#assign aDateTime = .now>
<#assign aDate = aDateTime?date>
current date: ${aDate?iso_utc}

如何使用freemarker'获得30天前'和'一年前'的约会?

3 个答案:

答案 0 :(得分:3)

得到了答案。 在下面找到免费标记代码:

<#assign currentDate = .now>
Current Date : ${currentDate?date}<br>
<#assign numberOfDays = 30?long>
<#assign timeInMillisecond = (1000 * 60 * 60 * 24 * numberOfDays) >
<#assign aDate = currentDate?long - timeInMillisecond?long>
<#assign Diff = aDate?long>
<#assign thirtyDaysBeforeDate = Diff?number_to_date>
<br>Date before ${numberOfDays} Days : ${thirtyDaysBeforeDate}<br>
<br>Date before ${numberOfDays} Days in UTC format : ${thirtyDaysBeforeDate?iso_utc}<br>

答案 1 :(得分:0)

您可以将日期/时间/日期时间转换为自aDateTime?long以来的纪元以来的毫秒数。然后,您可以相互减去这些值,并将它们除以一天或一年的长度。如果您多次需要此功能,请将其打包为#function#macro

答案 2 :(得分:0)

您可以在Freemarker中为此创建函数:

<#function dateDiff date days>
    <#assign timeInMilliseconds = (1000 * 60 * 60 * 24 * days) >
    <#assign aDate = date?long - timeInMilliseconds?long>
    <#return aDate?number_to_date>
</#function>

您可以这样使用:

date: ${date?string('dd.MM.yyyy HH:mm:ss')}
date 10 days ago: ${dateDiff(date, 10)?string('dd.MM.yyyy')}

这将打印出来:

date: 25.07.2018 20:01:35
date 10 days ago: 15.07.2018