Angular Material 的日期选择器。 我想将日期选择器的格式更改为 dd-mm-yyyy。
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.formatDate = function(date) {
return moment(date).format('YYYY-MM-DD');
};
});
但我想要为应用程序中的所有日期选择器进行一次性配置。 我认为这个例子是针对单个日期选择器的。
最佳答案
您可以通过下面的代码轻松完成,
app.config(function($mdDateLocaleProvider) {
$mdDateLocaleProvider.formatDate = function(date) {
if (!date) {return '';}
else{
return moment(date).format('DD-MM-YYYY');
}
};
$mdDateLocaleProvider.parseDate = function(dateString) {
var m = moment(dateString, 'DD-MM-YYYY', true);
return m.isValid() ? m.toDate() : new Date(NaN);
};
});
关于angularjs - 我想将 md-datepicker 的格式设置为 dd-mm-yyyy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42093884/