【问题标题】:AngularJS UI datePicker always one day offAngularJS UI datePicker 总是休息一天
【发布时间】:2014-10-01 13:56:18
【问题描述】:

我在应用程序上使用 AngularJS UI datePicker,表单上有三个日期输入。无论选择和保存什么日期,这一天总是提前一天。我已经尝试了日期格式的所有组合,并搜索了有关此主题的每个论坛帖子,但没有任何效果。选择日期后,我可以对代码进行哪些更改以获得正确的日期?

JS

configAppControllers.controller('deadLineDatePickerCtrl', ['$scope', '$timeout',
function ($scope, $timeout) {
    $scope.today = function () {
        $scope.dt = new Date();
    };
    $scope.today();

    $scope.clear = function () {
        $scope.dt = null;
    };

    // Disable weekend selection
    $scope.disabled = function (date, mode) {
        // return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));
    };

    $scope.toggleMin = function () {
        $scope.minDate = $scope.minDate ? null : new Date();
    };
    $scope.toggleMin();

    $scope.open = function ($event, id) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.opened = true;
        $timeout(function () {
            $("#" + id).focus();
        });
    };

    $scope.dateOptions = {
        formatYear: 'yy',
        startingDay: 1
    };

    $scope.initDate = new Date('2012/03/21');
    $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
    $scope.format = $scope.formats[3];
  }
]);

HTML

<div class="input-group deadline">
  <input name="deadLine" id="deadLine" type="text" class="form-control" 
    data-ng-required="true"
    datepicker-popup="{{format}}"
    data-ng-model="programDetails.deadline" 
    is-open="opened" 
    min-date="programDetails.startDate"
    datepicker-options="dateOptions"
    date-disabled="disabled(date, mode)" 
    close-text="Close"
    show-button-bar="false"/>
    <span class="input-group-btn">
        <button type="button" class="btn btn-default" data-ng-click="open($event)"><i
            class="fa fa-calendar"></i></button>
   </span>

【问题讨论】:

    标签: jquery angularjs bootstrap-datepicker


    【解决方案1】:

    如果有人在使用 datePicker 插件时遇到同样的问题,我可以通过使用 getTimezoneOffset() 方法来解决这个问题:

    user.deadline = formatDate(new Date(deadlineDate.getTime() + deadlineDate.getTimezoneOffset() * 60000));
    

    问题解决了!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多