我想将存储在我服务器上的营业时间与当前时间进行比较。
鉴于以下内容:
开始:09.00 结束:00.30
如果商店已开启或关闭,我想要结果。当当前时间不在给定的开始和结束时间时,商店关闭。
我到目前为止使用的代码不起作用。我收到错误:
TypeError:无法读取属性' 3'未定义的
firebase.database().ref("v3/standalonelist/bars").on('value', function(snapshot) {
$timeout(function() {
$scope.content = snapshot.val();
snapshot.forEach(function(childSnapshot) {
$scope.data1 = [childSnapshot.val()];
$scope.locations1 = $scope.data1
$scope.locations1.forEach(function(item) {
$scope.getMinutes = function(str) {
var time = str.split('.');
return time[0] * 60 + time[1] * 1;
}
$scope.getMinutesNow = function() {
var timeNow = new Date();
return timeNow.getHours() * 60 + timeNow.getMinutes();
}
var a = new Date();
var day = a.getDay(); // 3
var now = $scope.getMinutesNow();
console.log(item.opens[day]); // returns 09.00
var start = $scope.getMinutes(item.opens[day]); // 09.00
var end = $scope.getMinutes(item.closes[day]); // 01.20
if (start > end) {
end += $scope.getMinutes('24.00');
}
if ((now > start) && (now < end)) {
$scope.$apply(function() {
$scope.open = true; // Store is open
})
} else {
$scope.open = false; // Store is closed
}
})
})
答案 0 :(得分:0)
我认为你让它变得有点复杂......
all_from
这是一个工作小提琴:http://jsfiddle.net/L770r2qk/
您可能还想考虑不同的日子,例如从深夜到凌晨运营的商家。为此,您还需要使用def all_from(datab,university):
# Create flag to see if there are results
result = False
for k in datab.keys():
# Knowing that university will always be in the same column
if university == datab[k][2]:
result = True
print datab[k]
if not result:
print 'Nobody goes to University there!'
datab = dict()
datab[1] = ['walker','matthew','uuc',1]
datab[2] = ['stewart','rory','uum',2]
datab[3] = ['toner','kevin','qub',4]
datab[4] = ['hughes','johnny','uuj',1]
datab[5] = ['douglas','kenny','uuc', 3]
datab[6] = ['hooks', 'peter','qub',1]
datab[7] = ['campbell','vicky','uuj',2]
datab[8] = ['crooks','laura','uum',4]
datab[9] = ['irwin','emma','uuc',3]
datab[10] = ['patterson','steve','uuc',1]
university = (raw_input('Enter the University here: '))
all_from(datab,university)
和var today = new Date(),
open = "Open",
closed = "Closed",
display = document.getElementById('display'),
start = new Date(), end = new Date();
// Set the opening hour:minutes
start.setHours(10);
start.setMinutes(5);
// Set the closing hour:minutes
end.setHours(18);
end.setMinutes(30);
// Check the time based on milliseconds
if (
today.getTime() >= start.getTime() &&
today.getTime() < end.getTime()
) {
display.innerHTML = open;
} else {
display.innerHTML = closed;
}
。然后,您可能还想考虑月份更改,然后使用start.setDate(12)
和end.setDate(13)
。
这些额外的功能取决于您,我将让您将它们实施到示例中。