4

I have a fairly simple question about these 2 templating engines.
I'm trying to make a future/past date, a fixed time from now, e.g. 18 months ago, or tomorrow.

I know that it is possible to do this with a java date object inside a velocity/freemarker template (something like $date.add(2,-18)), but I would like to do this with DateTool or freemarker core.

This is something that I see as purely presentational (just think at the default dates you see in flight booking forms), so I can't see any reason why a templating engine shouldn't be able to do this.

Is it possible though? If so, how?

Razor
  • 27,418
  • 8
  • 53
  • 76

7 Answers7

10

I know this is post is really old, but for people from the future still looking for an answer: Date manipulation can be done converting dates to/from milliseconds:

${(mydate?long - 18 * 86400000)?number_to_date?string("yyyy-MM-dd")}

for example would subtract 18 days from mydate. (Note that 86400000 is the amount of milliseconds per day)

Stefan Haberl
  • 9,812
  • 7
  • 72
  • 81
  • One thing to note: `?long` is not a documented date builtin, it is on the expert builtins page: https://freemarker.apache.org/docs/ref_builtins_expert.html – joshwhatk May 16 '19 at 15:04
2

I found that per @Stefan Haberl, ?long does work on dates to get the same value as java.util.Date.getTime() as a Long. However, I needed a little more explanation to be able to compare dates or modify dates.

Here's the gist:

  1. ?long = java.util.Date.getTime() returns epoch time in milliseconds
  2. At this point, you can add/subtract any number of milliseconds from that number for your manipulation

I like working in seconds instead of milliseconds (less unnecessary zeros, I don't care about milliseconds, etc.), which looks like this:

[#function convertToUnix date]
  [#return (date?date?long / 1000)]
[/#function]

[#-- Output Unix Timestamp --]
${convertToUnix(.now)}

At this point, 86400 = 1 day (because we are in "seconds" now), so you can simply add/subtract that to manipulate the date.

[#assign
  day = 86400
  week = 7 * day
  avgMonth = 365.25 / 12 * day

  testingEndOfDay = convertToUnix(.now) < (convertToUnix(sameDay) + day)
  testingYesterday = convertToUnix(.now) < (convertToUnix(yesterday) + day)
]
${testingEndOfDay?c} # true, .now is less than the end of the day
${testingYesterday?c} # false, .now is greater than the end of yesterday

Note: I am ignoring the time of day, we received dates that started at 12:00AM and wanted to check against .now for the end of the day.

Now, if I want to get a date back from the Unix format (in seconds), I can convert it back using the ?number_to_date builtin

[#assign
  nowAsUnix = convertToUnix(.now)
  prettyDate = (nowAsUnix * 1000)?number_to_date
]

Note: I'm open to edits/improvements as I'm not sure why much of this was required ¯\_(ツ)_/¯

joshwhatk
  • 156
  • 1
  • 7
1

For freemarker, perhaps : Built-ins for dates.

Istao
  • 7,425
  • 6
  • 32
  • 39
  • I had already seen that doc page, and I cannot find any reference to date manipulation, just how to format them, perhaps I missed it? – Razor Apr 09 '10 at 15:31
  • Sorry, I think you need to write your own freemarker TemplateMethodModelEx for that... see this post : Calculate days between two dates at http://thread.gmane.org/gmane.comp.web.freemarker.user/4292 – Istao Apr 09 '10 at 16:45
  • That's something different from what I need, but it doesn't look like freemarker has any better date manipulation possibilities, when compared to velocity tools. I'm guessing here that it's not possible to do what I need with this engine either. – Razor Apr 10 '10 at 14:06
1

In Velocity Tools there is no such method.

serg
  • 109,619
  • 77
  • 317
  • 330
1

You can do it in Velocity too, but not with the DateTool (that belongs the Velocity Extras anyway).

It is a good practice to have a "Format" object with various utilities that are practical for presentational purposes only (you can see the various frameworks that use Velocity how they have "Format" objects).

So your code would look like $format.dateFromNow(), and there would be still a presentational approach.

A. Ionescu
  • 2,138
  • 16
  • 11
0

You can write your own methods to use in FreeMarker: http://freemarker.sourceforge.net/docs/pgui_datamodel_method.html

build a DataAddMethod that executes this logic.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
0

Just in case that helps someone, posting my result for Freemarker, based on previous answers:

<#function minusSeconds date, numSec >
    <#return (date?long - numSec * 1000)?number_to_datetime?iso_utc
</#function>
gce
  • 1,563
  • 15
  • 17