How to format date_time output nicely?
I'm trying to output the post date of a message in a nice way (best of course would be relative times for the past 7 days, e.g. "7 minutes ago" and "12.04.2012 17:34" else, but that's something for later). I've read the freemarker documentation ( http://freemarker.sourceforge.net/docs/ref_builtins_date.html#ref_builtin_date_datetype ), but trying both
${message.post_time?date} or ${message.post_time?time} just returns an error รก la:
The string doesn't match the expected date/time format. The string to parse was: "2012-04-17T15:16:28+00:00". The expected format was: "MMM d, yyyy".
...as a trial I've also did ${message.post_time?string.medium} to no avail:
Expected hash. message.post_time?string evaluated instead to freemarker.template.SimpleScalar on line 21, column 31 in preview.
Any idea about how to achieve some pretty formatted output of date_time data?
Hi Claudius,
Messing with dates in FreeMarker can be tricky, but in this case perhaps it's not necessary?
Our REST API supports a parameter called restapi.response_style that when set to view, will provide additional details in the response such as UI-style URLs, as well as UI-style dates and times. Here's a sample call:
/restapi/vc/threads/recent?restapi.response_style=view
If you look at a time or date in the response, you'll notice the element now has some additional attributes:
<post_time type="date_time" view_date="04-17-2012" view_time="01:44 AM" view_friendly_date="11 hours ago">2012-04-17T08:44:31+00:00</post_time>
So now you have the nicer version of the time and date in the response, and you'll notice that it even includes the friendly (relative) date version if you have that enabled for your community. The nice thing about this, is that it also respects the time zone preferences of the user. When you use the rest context object in a custom component, this parameter is already included for you, so you can take advantage of it without any further action.
Then to access these attributes, you can do this:
${message.post_time.@view_date} ${message.post_time.@view_time} ${message.post_time.@view_friendly_date}
If this doesn't help, let me know and I can try to help you work through the Freemarker.
Regards,
Adam