Unable to format default MySQL datetime

前端 未结 5 1277
礼貌的吻别
礼貌的吻别 2020-12-10 01:01

My dates come out of the database looking like this: 2013-11-21 17:43:20

I\'m trying to user Angular\'s date filter to turn them into something prettier

5条回答
  •  孤城傲影
    2020-12-10 01:40

    You need to convert your date string to something supported by Angular, like ISO 8601 format. You could convert it like this:

    $scope.Object.created = new Date($scope.Object.created).toISOString();
    

    Live demo here (click).

    To do this on the fly, you need a custom filter. Live demo here (click).

    Markup:

    {{Object.created | dateToISO | date:'shortDate'}}

    JavaScript:

    app.filter('dateToISO', function() {
      return function(input) {
        return new Date(input).toISOString();
      };
    });
    

    Update:

    Here's a simple way to convert your date manually (firefox):

    app.filter('badDateToISO', function() {
      return function(badTime) {
        var goodTime = badTime.replace(/(.+) (.+)/, "$1T$2Z");
        return goodTime;
      };
    });
    

提交回复
热议问题