Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: How to format ISO8601 date format?

I am using bootstrap-datetimepicker 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, it should be for format Oct 28, 2010 8:40:23 PM

What is that I am missing?

like image 209
daydreamer Avatar asked May 08 '13 23:05

daydreamer


2 Answers

I don't understand why no one provided the simple answer of using the correct format in the filter?

{{item.date | date:'yyyy-MM-ddTHH:mm:ssZ'}}

That will format as ISO-8601

like image 66
Robbo Avatar answered Sep 19 '22 11:09

Robbo


For now, I have created a filter

angular.module('customFilters', []).
  filter('dateInMillis', function() {
    return function(dateString) {
      return Date.parse(dateString);
    };
  });

added as dependency in app.js as

var app = angular.module('myApp', [
  '$strap.directives', 'ngCookies', 'categoryServices', 'transactionServices',
  'customFilters'
]);

and in HTML used it as

<!-- added dateInMillis to pass to date to filter Angular way -->
<td>{{ transaction.created_on | dateInMillis | date: 'medium'}}</td>

and that presents date on HTML as

May 8, 2013 5:14:36 PM 

If you know a better idea, please let me know

like image 23
daydreamer Avatar answered Sep 20 '22 11:09

daydreamer