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:
?long
= java.util.Date.getTime()
returns epoch time in milliseconds
- 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 ¯\_(ツ)_/¯