自定义angularjs过滤日期时间格式额外字符?

时间:2015-03-26 16:06:20

标签: javascript angularjs

我创建了这个自定义角度js过滤器来格式化日期和时间,但我有问题格式输出是'03 / 25/2015 19:03 PM3 / P3'这个3 / p3来自哪里?我的格式应该是MM / DD / YYYY HH:MM AM / PM?

javascript

app.filter('formatDateAndTime', function () {
    return function (input){
        if (moment.utc(input).local().format('MM/DD/YYYY HH:MM AM/PM') === 'Invalid date')
            return ' ';
        else
            return moment.utc(input).local().format('MM/DD/YYYY HH:MM AM/PM');
    };
});

2 个答案:

答案 0 :(得分:1)

MM/DD/YYYY HH:MM AM/PM

应该是

MM/DD/YYYY HH:MM A

AM/PM在英语中的格式表示(上午/下午)(月#)/ P(月#)。请参阅格式化文档here

答案 1 :(得分:1)

moment.utc(input).local().format('MM/DD/YYYY HH:MM A');

对于上午/下午,您必须使用A

angular.module("app",[])
.controller("MainCtrl", function($scope) {
   $scope.query = moment();
})
.filter('formatDateAndTime', function () {
    return function (input){
        if (moment.utc(input).local().format('MM/DD/YYYY HH:MM AM/PM') === 'Invalid date')
            return 'invalid ';
        else
            return moment.utc(input).local().format('MM/DD/YYYY HH:MM A');
    };
});
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="http://momentjs.com/downloads/moment.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="MainCtrl">
{{query | formatDateAndTime}}

</body>
</html>