我正在使用 Angular Material 日期选择器。我的问题是,当我向 Web Api Controller 发送日期时,我得到的日期小于我在表单中选择的日期。我认为这是由于日期值未本地化所致。我想知道的是如何在 angular js
中本地化 dateHTML:
<div ng-controller="AppCtrl" style='padding: 40px;' ng-cloak>
<md-content>
<h4>Standard date-picker</h4>
<md-datepicker ng-model="myDate" md-placeholder="Enter date"></md-datepicker>
</md-content>
</div>
Controller :
angular.module('datepickerBasicUsage',
['ngMaterial', 'ngMessages']).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());
$scope.onlyWeekendsPredicate = function(date) {
var day = date.getDay();
return day === 0 || day === 6;
}
});
最佳答案
为了跨浏览器获得一致的结果,最好使用 moment.js
否则你也可以使用toLocaleString() .
最后,您还可以编写自己的服务,该服务将获取 UTC 时间并根据用户区域设置应用必要的偏移量。
关于javascript - 本地化 Angular Material datepicker 中的日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37516825/