Chris Chris - 2 months ago 18
AngularJS Question

Angular date filter not working with milliseconds (string or number)

I have something like this in my view:

{{1288323623006 | date:'%d/%m/%Y %I:%M:%S %p'}}

But it throws me an error in my console like this:
enter image description here

When I convert the milliseconds to a date object in my controller, like this:

$scope.myDate = new Date(1288323623006);

and in my view:

{{myDate | date:'%d/%m/%Y %I:%M:%S %p'}}

then it shows
29/10/2010 06:40:23 AM
as expected.

Here is an example from Angular Docs which throws the above error in my console.

I can convert the date to an object and pass it to the view, but I am interested to manage it this way, cause I don't want to use such a fixes.

I haven't change anything in my I18nAdapter.

Anyone had similar issue?


I have recently solved this issue and now I am posting my solution.

It turned out the angular's filters did not work because of localization's filters which were used instead.

What I did was disabling the localized date filters by commenting these lines inside our i18n.js library:

angular.module('app.filters.I18nAngular', []).filter('translate', function() {
   return function(key, obj) {
      return i18nAdapter.translate(key, obj);
}).filter('number', function() {
   return function(value, options) {
      return i18nAdapter.formatNumber(value, options);
}).filter('currency', function() {
   return function(value) {
      return i18nAdapter.formatCurrency(value);
// .filter('date', function() {
// return function(value, format) {
// return i18nAdapter.formatDate(value, format);
// };
// });

The i18nAdapter has it's own formatting and filters, similar to angular, but in our case we are just fine using those that angular provides.