I am trying to match with current time with predefined time but i got stuck in matching because those are in string . This is my Code
$scope.TimeString = "17:00-21:00,7:00-10:00,9:45-12:00";
$scope.currentTime = $filter('date')(new Date(), 'HH:mm');
$scope.IsItemAvailable = function (timeString) {
var tempTimeArray = "";
tempTimeArray = timeString.split(",");
for (var i = 0; i < tempTimeArray.length; i++) {
var tempTime = tempTimeArray[i];
var itemTime = tempTime.split("-")
var fromTime = $filter('date')(itemTime[0], 'HH:mm');
var toTime = $filter('date')(itemTime[1], 'HH:mm');
if ($scope.currentTime >= fromTime && $scope.currentTime <= toTime) {
$scope.isAvailable = true;
break;
}
else {
$scope.isAvailable = false;
}
}
it's returning false always.Any please help me to acheive it.
If you can convert your times to zero-padded: 09:55
then you can compare it as strings(if you have not midnight included intervals).
Else you can convert hh:mm
to minutes number and then compare:
var cur = $scope.currentTime.split(':');
var from = itemTime[0].split(':');
var to = itemTime[1].split(':');
cur = cur[0]*60+ cur[1];
from = from[0]*60+from[1];
to = to[0]*60+ to[1];
if (cur >= from && cur <= to) {
// available
}
Some DRY resolved:
var times = [$scope.currentTime, itemTime[0], itemTime[1]];
times.map(interval => interval.split(':'))
.map(interval => interval[0]*60 + interval[1]);
if (times[0] >= times[1] && times[0] <= times[2]) {
// available
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.