angularjs日期和时间计算彼此不匹配

时间:2015-07-04 07:22:28

标签: javascript html angularjs

编写代码以计算angularjs中的日期和时间。在这里我们有开始和结束日期。开始时间和结束时间。因此我们需要总天数和总小时数。但是o / p有一些错误。 假设我们选择开始日期为10日,开始时间为下午5点,结束日期为13,结束时间为下午2点。

结果应为2天21小时。但我没有得到确切的结果,我无法计算"每个块预订"作为最终结果..plz建议

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
<script>
angular.module('test', [])
.controller('Test', function($scope) {
  $scope.inputs = [{}]; // default 1 sets

  $scope.add = function() {
    $scope.inputs.push({});
  }

  $scope.getTotal = function(a) {

    var total = 0, total2 = 0, all_final_days =0;
      angular.forEach($scope.inputs, function(value) { // loop over array to process all items
      total += (value.param2 - value.param1)/1000/60/60/24;

    });

     function fun3(x) // here we getting total hours and if value is negative then we subtract 1 day from total days
        {
        if(x<0){
            total2 = parseInt(total) - 1;
            return total2;
            }
      }
    all_final_days = fun3(a);
    return all_final_days;
  }

  $scope.getTotalTime = function() {
    var totalHours = 0
    var result=0;
    angular.forEach($scope.inputs, function(value) { // loop over array to process all items
      totalHours += (value.param4 - value.param3)/1000/60/60;
    });
            //alert("final hours is:"+totalHours);  
     <!-- TotalHourTwo = $scope.getTotal(totalHours);
     // return totalHours;
     // return TotalHourTwo;-->
      $scope.getTotal(totalHours);
      return totalHours;

  }

// coding for remove button
  $scope.remove = function(item) {
    angular.forEach($scope.inputs, function(value, key) {
      if (value == item) {
        $scope.inputs.splice(key, 1);
      }
    });
  }
});
</script>
</head>

<body>
<div ng-app='test' ng-controller='Test'>
  <div ng-repeat='input in inputs' ng-init="input.param1 = input.param2 = input.param3 = ''" style="background:#CCC;padding:10px;margin-bottom:10px;"> Start Date
    <input type='date' ng-model='input.param1'>
    <!-- assign ng-model to local variable --> 
    End Date
    <input type='date' ng-model='input.param2'>
    = {{total = (input.param2 - input.param1)/1000/60/60/24}} <br />
    Start Time
    <input type="time" ng-model='input.param3' />
    End Time
    <input type="time" ng-model='input.param4' />
    = {{TotalHourTwo = (input.param4 - input.param3)/1000/60/60}} <br />
    <br>
    <button type='button' ng-click='remove(input)'>Remove</button>
  </div>
  <div>
    <button type='button' ng-click='add()'>Add</button>
  </div>
  <br>
  <br>
  <div>TotalDays = {{getTotal()}}</div>  <!-- finaly total days from all booking print here -->
  <div>TotalTime = {{getTotalTime()}}</div> <!-- finaly total hours from all booking print here -->
</div>
</body>
</html>

0 个答案:

没有答案