【问题标题】:AngularJS: mdDatepicker is one day-offAngularJS:mdDatepicker 休息一天
【发布时间】:2018-04-21 16:54:47
【问题描述】:

我正在使用 Angularjs Material datepicker 扩展

这是一个简单的用法示例:

<md-datepicker ng-model="$ctrl.date"></md-datepicker>

在我的应用配置中:

$mdDateLocaleProvider.formatDate = function (date) {
  if (!date) {
    return '';
  }
  return moment(date).format('YYYY-MM-DD');
};

这是我面临的一个问题 - 当通过日历选择日期时,它会返回正确的值, 但是当我输入一个确切的日期时,例如 2012-12-12 它给了我一天假的价值 Tue Dec 11 2012 00:00:00 GMT-0500 (EST)

附:这是我发现有这样一个问题的Codeopen

【问题讨论】:

    标签: javascript angularjs datepicker timezone angularjs-material


    【解决方案1】:

    这是 javascript 中的一个已知错误 例子: new Date ('2012/03/21'); --> Wed Mar 21 2012 00:00:00 GMT-0400 (Eastern Daylight Time)

    new Date ('2012-03-21'); --> Tue Mar 20 2012 20:00:00 GMT-0400 (Eastern Daylight Time)

    我能给你的最好建议是将- 更改为/

    试试这个

    HTML

    <div ng-app="MyApp" ng-controller="AppCtrl">
      <md-content>
        <md-datepicker ng-model="myDate" md-placeholder="Enter date" ng-change="change(myDate)"></md-datepicker>
      </md-content>
    </div>
    

    JS

    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()); 
    console.log($scope.myDate.toLocaleDateString(), $scope.myDate.toUTCString());
    
           $scope.change = function(data) { 
            console.log($scope.myDate.toLocaleDateString());
            console.log( $scope.myDate.toUTCString());     
          }
    
    })
    
    
    .config(function($mdDateLocaleProvider) {
      $mdDateLocaleProvider.formatDate = function(date) {
        return moment(date).format('YYYY/MM/DD');
      };
    });
    

    【讨论】:

    • 它仍然给我一天假的价值
    • 您当前的时区是什么?
    • 东部时区
    • 嗨@LuninRoman 我认为这确实是一个已知问题,请参考这里github.com/angular/material/issues/4636
    • 感谢您的建议,但我已经阅读了整个主题,但没有找到任何可以帮助我的内容
    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2019-05-11
    相关资源
    最近更新 更多