Forum Discussion

Claudius's avatar
13 years ago

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 l...
  • AdamN's avatar
    13 years ago

    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