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 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

  • AdamN's avatar
    AdamN
    Khoros Oracle

    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

      • Claudius's avatar
        Claudius
        Boss

        A quick follow up here: Why is the "view_friendly_date" parameter empty if the date_time is more than some 4 weeks ago, e.g. here:

        <last_edit_time type="date_time" view_date="08-02-2012" view_time="15:45" view_friendly_date="">
        2012-02-08T14:45:31+00:00
        </last_edit_time>

         I need to check in my script for this case and output the date instead then. I would have expected "view_friendly_date" to be set in all cases. I would even consider it a design flaw if not even a bug. What do you think?





  • FYI, I know this is answered, but I wanted to share some work we did recently with dates and Freemarker in case it may help someone in the future:

     

    <#assign date = thread.messages.topic.last_edit_time?datetime("yyyy-MM-dd'T'HH:mm:ss'+00:00'") />
    <META NAME="date" CONTENT="${date?string("MM/dd/yyyy HH:mm:ss")}">
    <!-- Outputs 04/19/2012 11:39:30 -->
    <META NAME="lastupdatedate" CONTENT="${date?string("yyyyMMddHHmmss'+00''00'''")}"> <!-- Outputs 20120419113930+00'00' -->
    <META NAME="searchdate" CONTENT="${date?string("dd MMM yyyy HH:mm:ss")}">
    <!-- Outputs 19 Apr 2012 11:39:30 -->