【问题标题】:'Get 30 days ago' and 'one year ago' date in freemarkerfreemarker 中的“获取 30 天前”和“一年前”日期
【发布时间】:2015-09-27 07:08:22
【问题描述】:

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

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

如何使用 freemarker 获取“30 天前”和“一年前”日期?

【问题讨论】:

  • 我假设你通过在 aDateTime 上做数学来做到这一点。

标签: freemarker


【解决方案1】:

得到了答案。 在下面找到 freeemarker 代码:

<#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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您可以为此在 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
      

      【讨论】:

        猜你喜欢
        • 2019-04-10
        • 1970-01-01
        • 2017-10-23
        • 2012-02-09
        • 1970-01-01
        • 2012-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多