0

i'm trying to make a app which is base on time differnce and date difference, and both date and time is input by the user,and the time format both cases in 24 hr.when i try to subtract these value it give wrong ans.

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="">
Time1:<br><input type="text" pattern="([0-1]{1}[0-9]{1}|20|21|22|23):[0-5]{1}[0-9]{1}" ng-model="time1"><br>
<br>
Time2:<br><input type="text" pattern="([0-1]{1}[0-9]{1}|20|21|22|23):[0-5]{1}[0-9]{1}" ng-model="time2">
<p> Your Time is ans is{{time1-time2}} </p>
</div>

</body>
</html>
Codexxx
  • 63
  • 9

1 Answers1

0

Your solution is using the input type time for html and format the calculation result as described in AngularJS:How to print out duration time with format? by Aung Khaing Oo.

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp">
Time1:<br><input type="time" id="time1" name="time1" ng-model="time1" placeholder="HH:mm" required /><br>
<br>
Time2:<br><input type="time" id="time2" name="time2" ng-model="time2" placeholder="HH:mm" required />
<p>
time1:{{time1}}<br/>
time2:{{time2}}
</p>
<p> Your Time is ans is {{(time1-time2)| makePositive  | date: 'HH:mm': 'UTC'}} </p>
</div>
<script>
var app = angular.module('myApp', []);
app.filter('makePositive', function() {
return function(num) { 
    if(num < 0) {
       todayDate = new Date(num);
       todayDate.setDate(todayDate.getDate()+1);
       num = todayDate;
    }
    return num; 
}
});
</script>
</body>
</html>

Update: I added a custom filter to solve the time1 < time2 problem. I took the solution from here: Display absolute value angularjs

Update2: changed the filter by just adding one day if the difference between time1 and time2 is negative. Used angularjs - calculate a date plus one day as base for my adaption.

Community
  • 1
  • 1
Alexander
  • 1,356
  • 9
  • 16
  • Thank you for your response ,can you please suggest me how i can make this concept ,if time1 – Codexxx Feb 16 '17 at 12:50
  • Sorry it absolute value but my question is different please check my qns.suppose user add time1 =6:00 AM and time2 =07:20AM in logic the 6:00 AM goes to as 30:00 and now the subtraction b/w 30:00 and 07:20.But if the value is greater than 07:20 it's subtract normal. – Codexxx Feb 16 '17 at 13:16