【问题标题】:How to Increase Date after the time in Timepicker passes midnight?Timepicker中的时间经过午夜后如何增加日期?
【发布时间】:2016-01-07 10:52:14
【问题描述】:

我有一个startDate 模型,我已绑定到引导日期选择器和时间选择器。当我增加时间选择器中的时间并且它经过午夜时,我需要让模型也增加它的日期。以下是我现有的代码。

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);

angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function($scope) {

  $scope.hstep = 1;
  $scope.mstep = 15;

  $scope.ismeridian = true;
  $scope.toggleMode = function() {
    $scope.ismeridian = !$scope.ismeridian;
  };

  $scope.today = function() {
    $scope.startDate = new Date();
  };

  $scope.startDateState = {
    opened: false
  }; /* Model to keep track if Popup is open */

  $scope.today(); /* Sets Date as Today Initially */
  $scope.showWeeks = false; /* Hides Week Numbers */
  $scope.minDate = new Date(); /* Disables past dates */
  $scope.format = 'dd MMM, yyyy'; /* Date Format Setting */
  $scope.dateOptions = {
    startingDay: 1,
    showWeeks: false,
  }; /* to be passed in the date-options directive */

  $scope.open = function() {
    $scope.startDateState.opened = true;
  }; /* Button Click Event to open Popup */

});
<!doctype html>
<html ng-app="ui.bootstrap.demo">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script>
  <script src="example.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>

  <div ng-controller="DatepickerDemoCtrl">
    <pre>Start date is: <em>{{startDate| date:"MM/dd/yyyy 'at' h:mma"}}</em></pre>
    <div class="row">
      <div class="col-md-6">
        <p class="input-group">
          <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="startDate" is-open="startDateState.opened" on-open-focus="false" min-date="minDate" show-button-bar="false" datepicker-options="dateOptions" ng-required="true" />
          <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="open()"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
        </p>
        <uib-timepicker ng-model="startDate" hour-step="hstep" minute-step="mstep" show-meridian="ismeridian"></uib-timepicker>
      </div>
    </div>
  </div>
</body>

</html>

如何解决这个问题?

【问题讨论】:

    标签: javascript angularjs angular-ui-bootstrap timepicker bootstrap-datetimepicker


    【解决方案1】:

    阿卡什,

    我解决问题的方法是在日期输入字段上使用 ng-change 指令。

    HTML

    <input ... ng-change="startDateChanged({{startDateTime}})"></input>
    

    您会注意到传递给控制器​​作用域函数的参数是一个文字表达式。这样做的原因是,我们可以比较 ng-model 日期对象的原始值,在它被时间选择器的向上或向下箭头修改之前。

    控制器

    var app = angular.module('plunker', ['ui.bootstrap']);
    
    app.controller('MainCtrl', function($scope) {
      var twelveAm = 0;
      var oneAm = 1;
      var elevenPm = 23;
      $scope.hStep = 1;
      $scope.mStep = 15;
      $scope.startDateTime = new Date();
      $scope.today = new Date();
    
      $scope.datePickerState = {
        startDateOpened: false
      };
    
      $scope.datePickerOptions = {
        "show-weeks": false
      };
    
      $scope.open = () => {
        $scope.datePickerState.startDateOpened = true;
      };
    
      $scope.startDateChanged = (originalModel) => {
        var nd = _.clone($scope.startDateTime);
        var od = new Date(originalModel);
    
        if (nd.getHours() === twelveAm) {
          if (od.getHours() === elevenPm) {
            nd.setDate(nd.getDate() + 1);
            $scope.startDateTime = nd;
          } else if (od.getHours() === oneAm) {
            nd.setDate(nd.getDate() - 1);
            $scope.startDateTime = nd;
          }
        }
      };
    });
    

    在 $scope.startDateChanged() 方法中,我首先创建了两个新的日期对象,一个保存原始日期时间的值(传入的表达式),另一个保存新的日期时间。然后我检查新日期时间 (nd) 的值是否为 12 AM。如果是这样,然后我检查原始日期时间(od)的值是 11 PM 还是 1 AM 并采取相应的行动(如果是 11 PM,这意味着我们按下的按钮是增加时间,因此增加日期,否则,我们将缩短时间,从今以后)

    我已经包含了一个 Plunker 来说明这个例子。

    https://plnkr.co/edit/dDMfpXmy3JBEjXzOca9v

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多