user3574939 user3574939 - 6 months ago 73
AngularJS Question

Converting JSON date(s) in AngularJS

I would like to convert dates retrieved from JSON string and format them yyyy-MM-dd. I am using the code below to format my the dates but oddly the dates are returning "12-31-1969" when the JSON string is for example "created_at": "2016-01-26T09:52:31Z" therefore the correct string would be 01-26-2016 not 12-31-1969.


<li class="mark">Created:</li>
<li class="mark"> {{item.created_at | jsonDate : 'MM-dd-yyyy' }}"</li>
<li class="mark">Updated:</li>
<li class="mark"> {{item.updated_at | jsonDate : 'MM-dd-yyyy'}}</li>


defaultPage.filter('jsonDate', ['$filter', function ($filter) {
return function (input, format) {
return (input) ? $filter('date')(parseInt(input.substr(6)), format) : '';


The parseInt(input.substr(6)) is unnecessary. From the documentation, the date argument to the filter function is described as:

Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.

In your case, it is in the ISO 8601 string format, so your filter line can just be:

return (input) ? $filter('date')(input, format) : '';

Here's a fiddle that shows it working: