2017-07-22 7 views
0

Google apiを使用して時間を返すajaxがあります。私が欲しいのは、時間が午前9時半から午後7時30分の間であれば、何らかのデータを出力することです。しかし、私の状態は機能していません。これに関する提案やアイデアはありますか?前もって感謝します。javascriptで時刻を取得する

この状態では機能しません。

if ((currentHours >= 9) && currentMinutes >= 30 && (currentHours <= 19) && currentMinutes <= 30) { } 

これは私のajaxです。

var loc = '35.652832, 139.839478' // Tokyo expressed as lat,lng tuple 
var targetDate = new Date() // Current date/time of user computer 
var timestamp = targetDate.getTime()/1000 + targetDate.getTimezoneOffset() * 60 // Current UTC date/time expressed as seconds since midnight, January 1, 1970 UTC 
var apikey = 'myKey' 
var apicall = 'https://maps.googleapis.com/maps/api/timezone/json?location=' + loc + '&timestamp=' + timestamp + '&key=' + apikey 

$.ajax({ 
    async: false, 
    type: "POST", 
    url: apicall, 
    success: function (response) { 
    var offsets = response.dstOffset * 1000 + response.rawOffset * 1000 // get DST and time zone offsets in milliseconds 
    var localdate = new Date(timestamp * 1000 + offsets) // Date object containing current time of Tokyo (timestamp + dstOffset + rawOffset) 
    var currentHours = localdate.getHours(); 
    var currentMinutes = localdate.getMinutes(); 
    var currentSeconds = localdate.getSeconds(); 
    currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes; 
    currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds; 
    currentTimeString = currentHours + ":" + currentMinutes; 
    console.log('currentimestring ' + currentTimeString); 
    // console.log(currentMinutes); 
    if ((currentHours >= 9) && currentMinutes >= 30 && (currentHours <= 21) && currentMinutes <= 30) { 
     _isSupportHoursResult = true; 
     console.log(_isSupportHoursResult); 
     if (_isSupportHoursResult) { 
     jpsupp = true; 
     console.log(jpsupp); 
     } else { 
     // window.location = "/jpprechatform/OffSupportRedirect.aspx?accessID=" + inAccessID; 
     jpsupp = false; 
     } 
    } else { 
     console.log('closing time'); 
    } 
    console.log(localdate.toLocaleTimeString()) // Display current Tokyo date and time 
    }, 
    error: function (xmlHttpRequest, error) { 
    _isSupportHoursResult = false; 
    }, 
    complete: function (response) { 
    } 
}); 
+0

は、あなたが考えてはいけません。 (currentHours = 9) '、' currentMinutes> = 30'、 '(currentHours <= 19)'、 'currentMinutes <= 30'は' currentHours'と 'currentMinutes'を矛盾させます。 – Priya

答えて

2

あなたの条件では(currentMinutes >= 30 && currentMinutes <= 30)が矛盾して使用されていますトリー。 これは問題のようです。あなたが使用することができます条件のカッコの配置が間違った場所にある、あなたの場合

if((currentHours > 9 && currentHours < 19) || (currentHours === 9 && currentMinutes >= 30) || (currentHours === 19 && currentMinutes <= 30)){} 
+0

ありがとう。それは –

0

try文は動作するはず:

if (currentHours >= 9 && currentHours <= 19) { 
    if (currentHours = 9 && currentMinutes < 30) { 
     return } 
    if (currentHours = 19 && currentMinutes > 30) { 
     return } 
    else { YOUR CODE HERE } 
} 

IF((currentHours> = 9 & & currentMinutes> = 30)& &(currentHours < = 19 & & currentMinutes < = 30)){}

+0

これは午前10時20分に動作しますか? – Dij

+0

@Dijいいえ、それはできません。ハ、私はコードを編集し、それは今動作するはずです – Varin

0
var minutesNow = currentHours*60+currentMinutes; 
if(minutesNow >= 9*60+30 && minutesNow <= 19*60+30){ } 
+0

でも動作します。すばらしい短いコード。 –

関連する問題