SharePoint Apps and Relative DateTime

When working with Apps, JSOM and the new SharePoint REST API there's always a challenge when it comes to displaying moments in time or dates. New in SharePoint 2013 is the display of Time Delta's like "A moment ago" or "2 days ago". The Newsfeed is a good example of this.

ISO8601 standard

SharePoint 2013's new cool REST API returns DateTime field values in ISO8601 format. This can be a value in the likes of "2013-01-29T13:37:00Z". Unfortunately there is no helper function (or I haven't found it yet) in the SharePoint JS Object Model to parse this format into a Time Delta.

Moment.js

This is where a tiny, free JavaScript library called moment.js comes in handy. With these very simple lines of code this library gives you the right output.

var spDate = "2013-01-29T13:37:00Z"; var deltaToNow = moment(spDate, "YYYY-MM-DDTHH:mm:ss Z").fromNow();

Displaying deltaToNow will output the relative value from the given date till the moment of execution.

Cheers!