admin管理员组

文章数量:1334887

The filter I created below works on Chrome but not Firefox. I don't understand why.

  myApp.filter('dateCustom', [ '$filter', function ($filter) {
    return function (input) {

      // input => 2014-05-13 15:04:48 

      if(angular.isDefined(input)){
        var d = new Date(input);
        var time = d.getTime();
        return $filter('date')(time,'dd/MM/yyyy');
      }
    }
  }]);

HTML :

<span> {{ project.date_created_at | dateCustom }} </span> 

Chrome

Firefox

The filter I created below works on Chrome but not Firefox. I don't understand why.

  myApp.filter('dateCustom', [ '$filter', function ($filter) {
    return function (input) {

      // input => 2014-05-13 15:04:48 

      if(angular.isDefined(input)){
        var d = new Date(input);
        var time = d.getTime();
        return $filter('date')(time,'dd/MM/yyyy');
      }
    }
  }]);

HTML :

<span> {{ project.date_created_at | dateCustom }} </span> 

Chrome

Firefox

Share Improve this question edited May 13, 2014 at 13:25 Steffi asked May 13, 2014 at 13:03 SteffiSteffi 7,08725 gold badges80 silver badges128 bronze badges 2
  • what is the input value? – Dalorzo Commented May 13, 2014 at 13:17
  • I still don't see the value just the object project.date_created_at which does not give us any clue of what is in it. – Dalorzo Commented May 13, 2014 at 13:28
Add a ment  | 

2 Answers 2

Reset to default 5

Firefox doesn't support a date in that format, you will have to replace the dash's with slashes first.

var d = new Date(input.replace(/-/g, '/'));

Converting a string to date may vary from one browser to another. If you are getting something like NaN on your date's it is because of the Date conversion.

The best way I found is to use MomentJS, a great tool to formating dates. Effectively after I used it worked in every browser. Just using:

moment($scope.date).format("DD/MM/YYYY");

本文标签: javascriptfilter date returns NaNNaNNaN in AngularJSStack Overflow