我使用Angular Material md-datepicker 。 ng-model的值看起来像这样:"2017-04-04T22:12:51.000Z"
(它被称为ISO格式或其他东西......)
嗯,问题是:如何创建全局配置,以便所有 md-datepicker 标记始终以特定格式格式化和解析ng-model值,让我们说YYYY-MM-DD
angular.module('MyApp')
.controller('AppCtrl', function($scope) {
$scope.myDate = new Date();
$scope.minDate = new Date(
$scope.myDate.getFullYear(),
$scope.myDate.getMonth() - 2,
$scope.myDate.getDate());
$scope.maxDate = new Date(
$scope.myDate.getFullYear(),
$scope.myDate.getMonth() + 2,
$scope.myDate.getDate()); })
.config(function($mdDateLocaleProvider) {
$mdDateLocaleProvider.parseDate = function(dateString) {
var m = moment(dateString, 'L', true);
return m.isValid() ? m.toDate() : new Date(NaN);
};
$mdDateLocaleProvider.formatDate = function(date) {
var m = moment(date);
return m.isValid() ? m.format('L') : '';
};
});

<link href="https://material.angularjs.org/HEAD/docs.css" rel="stylesheet"/>
<link href="https://material.angularjs.org/HEAD/angular-material.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-aria.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-messages.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.min.js"></script>
<script src="https://material.angularjs.org/HEAD/angular-material.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-114/assets-cache.js"></script>
<div ng-app="MyApp" ng-controller="AppCtrl">
<md-content>
<md-datepicker ng-model="myDate" md-placeholder="Enter date"></md-datepicker>
</md-content>
<big>ng-model value is: <strong>{{myDate}}</strong></big>
</div>
<p>
I want to format it this way: YYYY-MM-DD
</p>
&#13;
我将不胜感激任何帮助!!谢谢!
答案 0 :(得分:0)
但是,我已经通过创建自定义指令解决了我的问题。
希望这也有助于其他开发者。
Yohoo!
"/Users/giuseppecostantino/public_html/"
&#13;
angular.module('MyApp')
.controller('AppCtrl', function($scope) {
$scope.person = {
name: "John",
birthDate: "1990-01-01 00:00:00"
};
})
.directive('mdDatepicker', function () {
function link(scope, element, attrs, ngModel) {
var parser = function (val) {
val = moment(val).format('YYYY-MM-DD hh:mm:ss');
return val;
};
var formatter = function (val) {
if (!val) return val;
val = moment(val).toDate();
return val;
};
ngModel.$parsers.push(parser);
ngModel.$formatters.push(formatter);
}
return {
require: 'ngModel',
link: link,
restrict: 'EA',
priority: 1
}
});
&#13;
答案 1 :(得分:0)
这是一篇旧帖子,但以防万一:
md-datepicker对象只接受日期对象。您可以为ng-model创建临时日期变量,然后将日期变量转换为ng-change指令中的字符串。
<md-datepicker ng-model="tempDate" md-placeholder="Enter date" ng-change="person.birthDate = formatDate(tempDate)"></md-datepicker>
然后在控制器中实现formatDate()函数:
$scope.formatDate = function (date) {
return moment(date).format('YYYY-MM-DD hh:mm:ss');
}
我希望它有所帮助