2016-11-23 5 views
0

私のサーバに保存されている営業時間と現在の時間を比較したいと思います。店が開いているか閉じている場合、私は結果が欲しい00.30営業時間を比較するには?

開始:09.00 エンド

は、次のことを考えます。現在の時刻が指定された開始時刻と終了時刻にない場合、ストアは閉じられます。

これまで使用していたコードは機能しません。私はエラーを取得する:

TypeError: Cannot read property '3' of undefined

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

あなたは 'item.opens'と' item.closes'を定義しているようには見えません。 –

+0

はい、できます。私はfirebaseからデータを取得します。 var startの前にconsole.logが正しい時刻を返します。 – olivier

+0

あなたの投稿にデータを含める必要があります。 –

答えて

0

私はあなたがそれがなければならないよりも、それはもう少し複雑になっていると思う...ここ

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

が作業フィドルです:http://jsfiddle.net/L770r2qk/

遅い夜から早朝まで営業するビジネスなど、さまざまな日を考慮したい場合もあります。そのためには、start.setDate(12)end.setDate(13)も使用します。月の変更を考慮して、start.setMonth(10)end.setMonth(11)を使用する場合もあります。

これらの特別な機能はあなた次第です。私はそれらをこの例に実装することができます。

+0

でも、上記のように:end = 00.30? – olivier

+0

ちょっと待ってください... –

+0

@olivier答えを更新しました –

関連する問題