2017-02-16 88 views
0

我試圖做一個基於時間差異和日期差異的應用程序,並且日期和時間都是由用戶輸入的,並且時間格式都是在24小時內的情況。當我嘗試減去這些值時錯誤的答案。如何區分angularjs中的兩個時間值和日期值?

<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>

+0

請顯示相關代碼。 –

+0

對不起,現在代碼已經插入在html中。 – Anshuman

回答

0

你的解決方案是使用輸入類型時間爲HTML和由昂Khaing烏在AngularJS:How to print out duration time with format?描述格式化計算結果。

<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>

更新:我添加了一個自定義過濾器來解決時間1 < TIME2問題。我把該解決方案從這裏:Display absolute value angularjs

UPDATE2:只需添加如果有一天時間1和時間2之間的差爲負改變了過濾器。使用angularjs - calculate a date plus one day作爲我的適應基礎。

+0

謝謝你的迴應,你能告訴我怎麼做出這個概念,如果time1 Anshuman

+0

我使用自定義過濾器更新了我的答案。 – Alexander

+0

對不起,它的絕對值,但我的問題是不同的請檢查我的qns.suppose用戶添加time1 = 6:00 AM和time2 = 07:20AM在邏輯6:00 AM去30:00,現在減法B/W 30:00和07:20。但是,如果該值大於07:20,則會減去正常值。 – Anshuman

相關問題