cancel
Showing results for 
Search instead for 
Did you mean: 

Passing Full datetime through JSON

ashex
Champ in-the-making
Champ in-the-making
I'm passing the created and modified metadata for documents via JSON from the repo to share.
I found that I can't pass the raw format through JSON so I formatted it as dd MMM yyyy HH:mm:ss a '('zzz')'

The problem I'm seeing is I can process this with xmldate or datetime because I'm including the timezone (but if I remove the timezone the timestamp becomes arbitrary). What format would work instead? I don't think the above is a valid XML date which may cause problems since I'm building an atom feed from this.
4 REPLIES 4

rjohnson
Star Contributor
Star Contributor
You need to use ISO8601 format for all dates / datetimes. There are client side javascript functions to convert to / from this in Alfresco.util and server side ones as well. xmldate works with ISO8601.

Bob Johnson

ashex
Champ in-the-making
Champ in-the-making
To return the full date and time information I'll need to reformat it before presenting it through JSON then? Will xmldate preserve everything and be valid for JSON consumption?

rjohnson
Star Contributor
Star Contributor
Basically yes.

There are a couple of ways of doing this.

If you are returning a date from a repository webscript you can either convert the date in the javascript .get.js file or do it in the .json.ftl.

Doing it in javascript you would do something like


   result['issuedOnAt'] = utils.toISO8601(relatedItem.properties["fgitem:issuedOnAt"]);


and then you don't need to bother with xmldate in the ftl because the date is already formatted correctly.

Alternatively you can simply pass the document properties into the ftl template, optionally define a macro (just for ease of use) and convert the date format there.


<#macro dateFormat date>${xmldate(date)}</#macro>


Goes at the top of your ftl template and then you can do


"modifiedOn": "<@dateFormat item.properties.modified />",


To produce a correctly formatted ISO8601 date.

On the client side you have


var myDate = Alfresco.util.fromISO8601(jsonobject.datenode);


and


var my8601Date = Alfresco.util.toISO8601(javascript_date_object);


which convert dates either way.

Hope this helps

Bob Johnson

ashex
Champ in-the-making
Champ in-the-making
Thanks for the info, that will be super useful. I had tried using xmldate with the JSON template but it kept throwing an error. I'll take another look at it with the methods you've recommended.