日期选择器中本地化日期值

日期选择器中本地化日期值

本文介绍了在 Angular Material 日期选择器中本地化日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Angular Material 日期选择器.我的问题是,当我向 Web Api 控制器发送日期时,我得到的日期小于我在表单中选择的日期.我认为这是因为日期值没有被本地化.我想知道的是如何在angular js

本地化日期

HTML:

<md-内容><h4>标准日期选择器</h4><md-datepicker ng-model="myDate" md-placeholder="输入日期"></md-datepicker></md-content>

控制器:

angular.module('datepickerBasicUsage',['ngMaterial', 'ngMessages']).controller('AppCtrl', function($scope) {$scope.myDate = 新日期();$scope.minDate = 新日期($scope.myDate.getFullYear(),$scope.myDate.getMonth() - 2,$scope.myDate.getDate());$scope.maxDate = 新日期($scope.myDate.getFullYear(),$scope.myDate.getMonth() + 2,$scope.myDate.getDate());$scope.onlyWeekendsPredicate = 函数(日期){var day = date.getDay();返回日 === 0 ||天 === 6;}});
解决方案

为了在浏览器间获得一致的结果,最好使用 moment.js

否则你也可以使用 toLocaleString() .

最后,您还可以编写自己的服务,该服务将获取 UTC 时间并根据用户区域设置应用必要的偏移量.

I'm working with Angular Material date picker. My problem is that when I send a date to the Web Api controller, I get a date less than the date I have selected in my form. I think this is due to the fact that the date value is not being localized. What I want to know is that how to localize the date in angular js

HTML:

<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;
  }
});
解决方案

To get consistent result across browsers , its best to use moment.js

Otherwise you can also use the toLocaleString() .

Lastly you can also write your own service which will get UTC time and apply necessary offsets based on the user locale.

这篇关于在 Angular Material 日期选择器中本地化日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:56