日期过滤器无法正常工作

时间:2017-08-27 14:37:18

标签: angularjs date ionic-framework

我有一个我现在测试的应用程序,除了部分之外,一切都完美无缺。

日期过滤器每两年只能运行一次。 我检查Postman和BDD,日期是正确的,但我不明白为什么不显示它给我的日期" null"。

以下是截图和我的代码:enter image description here

我的观点:

private static Context mContext;

@Override
public void onCreate() {
    super.onCreate();
    Fabric.with(this, new Crashlytics());
    mContext = this;
    // register to be informed of activities starting up
    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

        @Override
        public void onActivityCreated(Activity activity,
                                      Bundle savedInstanceState) {

            // new activity created; force its orientation to portrait
            activity.setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    });
}
public static Context getContext(){
    return mContext;
}

我的过滤器:

<ion-list id="ficheJeune-list10">
  <ion-item id="ficheJeune-list-item22" ng-repeat="element in patient.cr track by $index" ng-click="showDescr(patient.id, element)">
    <div class="col col-sm-50">Compte Rendu du :</div>
    <div class="col col-sm-50 date">{{ element.date | dateOnly: "dd MMMM y" }}</div>
  </ion-item>
</ion-list>

mon json:

.filter('dateOnly', [
'$filter', function($filter) {
    return function(input, format) {
        return $filter('date')(new Date(input), format);
    };
}
    ])

我整天搜索了什么可能导致这种情况,但我找不到......

我提前感谢你

2 个答案:

答案 0 :(得分:1)

我认为您需要编写自定义地图,将月份名称从法语转换为英语。

在您的情况下maiavril失败。

Demo represents the issue

.filter('dateOnly', [
'$filter', function($filter) {

  var mappingMonth = {
    "avril": "april",
    "mars": "march",
    "mai" : "may"    
  }; // and so on

    return function(input, format) {

        input = input.split(" ")[0] + " " + mappingMonth[input.split(" ")[1]] + " " + input.split(" ")[2]

        return $filter('date')(new Date(input), format);
      };
     }
   ])

Fixed demo

答案 1 :(得分:0)

这是因为您的日期格式错误。例如,更改为maimay。 看流动中的演示:

var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope) {
    $scope.today = new Date();
    $scope.data = {
"id": 50,
"prenom": "Jonathan",
"cr": [
    {
        "date": "12 mars 2010",
     "lieu": "test"
    },
    {
        "date": "05 mars 2010",
        "lieu": "test"
    },
    {
        "date": "04 may 2006",
         "lieu": "test"
    },
    {
        "date": "27 apr 2006",
        "lieu": "test"
    },
    {
        "date": "14 aprww 2006",
        "lieu": "test"
    },
    {
        "date": "07 apr 2006",
         "lieu": "test"
    },
    {
        "date": "23 mars 2006",
        "lieu": "test"
    },
    {
        "date": "16 mars 2006",
         "lieu": "test"
    }
]
}
});
app.filter('dateOnly', [
'$filter', function($filter) {
    return function(input, format) {
        return $filter('date')(new Date(input), format);
    };
}
    ]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="datCtrl">
  <div id="ficheJeune-list-item22" ng-repeat="element in data.cr">
    <div class="col col-sm-50 date">{{ element.date | dateOnly: "dd MMMM y" }} </div>
  </div>
</div>