Reputation: 193
How to convert from date to POSIX time?
Date format isn't important("2016-02-12", "May 3, 2017 7:45:00 PM" and any other).
ps. I need to get past day,so i decided to convert it to posix and substract 86400.
Upvotes: 0
Views: 1719
Reputation: 3080
This is available as a built-in since 2011.
?long
now works for date, date-time or time values, and returns the milliseconds since the epoch (asjava.util.Date.getTime()
).
See: https://freemarker.apache.org/docs/versions_2_3_17.html
Upvotes: 2
Reputation: 31152
If you have the value as a String
(not a java.util.Date
), first you need to convert it to a value of type date or date-time (to java.util.Date
basically). That's done as myString?date
or myString?datetime
respectively, and the date_format
or datetime_format
FreeMarker configuration settings must match the format used in myString
. Alternatively, you can specify the format ad-hoc, for example as myString?datetime('MMM d, yyyy h:mm:ss a')
. See http://freemarker.org/docs/ref_directive_setting.html#topic.dateTimeFormatSettings for more information about date/time/datetime format strings. Also, as there's no time zone information in your sample strings, you must ensure that the time_zone
configuration setting is set to the time zone where the strings should be interpreted.
Now that you have the date or datetime value, you can convert it to milliseconds since the UNIX epoch as myDateOrDateTime?long
. POSIX uses seconds though, so then you need (millisSinceEpoch / 1000)?floor
.
So the longest possible expression is like myString?datetime('MMM d, yyyy h:mm:ss a')?long / 1000)?floor
, but certainly you will want make a FreeMarker #function
out of that.
Also note that FreeMarker by default prints numbers for humans, so it might prints something like 1,493,865,900
. To prevent that, apply ?c
at the end of the expression.
Upvotes: 1