AngularJS UI datePicker总是休息一天

时间:2014-10-01 13:56:19

标签: jquery angularjs bootstrap-datepicker

我在应用上使用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>

1 个答案:

答案 0 :(得分:1)

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

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

解决了问题!!