filter date returns NaN-NaN-NaN in AngularJS

偶尔善良 提交于 2019-12-07 02:24:20

问题


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


回答1:


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, '/'));



回答2:


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");


来源:https://stackoverflow.com/questions/23632059/filter-date-returns-nan-nan-nan-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!