AngularJS: How to format ISO8601 date format?

2,488 views
Skip to first unread message

Harit Himanshu

unread,
May 8, 2013, 7:24:57 PM5/8/13
to ang...@googlegroups.com
I am using [bootstrap-datetimepicker][1] and using ISO8601 datetime format as `yyyy-mm-ddThh:ii:ssZ` as mentioned in their options section

In my controller, I do

    transaction.date = $('.form_datetime input').val();
which sends the data to backend as (console.log)

    created_on: "Wed, 08 May 2013 23:18:32 -0000"

and saves in database as 

    2013-05-08 16:18:32-07

In my template, I do

    <td>{{ transaction.created_on | date:'medium'}}</td>

and I see the output on my HTML as 

    Wed, 08 May 2013 23:18:32 -0000

But as per the [Angular doc][2], it should be for format `Oct 28, 2010 8:40:23 PM`

What is that I am missing?


Luke Kende

unread,
May 8, 2013, 8:50:40 PM5/8/13
to ang...@googlegroups.com
Ran into that issue today actually. Though the docs say date can be a string, it doesn't work as expected, so convert to utc milliseconds first.  To fix, I created my own filter that gets angular's date filter injected:

angular.module('yourApp', []).
filter('myDate',
function($filter){
var angularDateFilter = $filter('date');
return function (dateString){
                             var d = new Date(dateString)
                             return angularDateFilter(d.getTime(), 'medium')
                        }
                 }
         )

then in template

<td>{{ transaction.created_on | myDate }}</td>
Reply all
Reply to author
Forward
0 new messages