【问题标题】:How to find date difference when using Angular UI datepicker使用Angular UI datepicker时如何查找日期差异
【发布时间】:2014-09-01 19:19:51
【问题描述】:

我正在使用 http://angular-ui.github.io/bootstrap/ 中给出的 AngularUI 日期选择器

我的页面中有两个日期选择器,单击按钮时需要验证两个值之间的日期差异是否大于 12 个月。 我没有找到任何可以使用的方法。

【问题讨论】:

    标签: angularjs angular-ui-bootstrap


    【解决方案1】:

    请看这里http://plnkr.co/edit/CeZrzGqD90cvaMLER6R5?p=preview 我做了消费当年有365天。

     angular.module('plunker', ['ui.bootstrap']);
        var DatepickerDemoCtrl = function($scope) {
    
          $scope.open = function($event, opened) {
            $event.preventDefault();
            $event.stopPropagation();
    
            $scope[opened] = true;
          };
    
          $scope.dateOptions = {
            formatYear: 'yy',
            startingDay: 1
          };
    
    
    function yearDiff(first, second) {
      if(second && first){
        return (second-first)/(1000*60*60*24*365);
      }
      else
      {
        return 0;
    
      }
    }
    
        $scope.$watch('start', function(i){
         $scope.diff =  yearDiff($scope.start, $scope.end)
          console.log($scope.diff);
        });
        $scope.$watch('end', function(i){
         $scope.diff =  yearDiff($scope.start, $scope.end)
          console.log($scope.diff);
        });
    
    
          $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
          $scope.format = $scope.formats[0];
        };
    

    【讨论】:

    • 感谢代码!当我在我的代码中实现它时,第二次第一次抛出会给出一个 NaN!
    • 这种方法奏效了!我的问题是我的日期是一个字符串。所以我做了一个新的 Date(mystring) 然后按照你的代码。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多