2014-02-18 57 views
1

該代碼將new Date()更改爲DayHourMinute
monday9AM45minutes010945
我用的是010945,我的代碼指定
如果VAR是>=010921 && <=011010
變化之間的background綠色else什麼`if(Date> = Date1 && <= Date2){do this}`dosn't work

但沒有任何反應,如果我用alert(Time)它給人的消息010945

如何解決這個問題?

代碼:

function one() { 
    now = new Date(); 
    hour = "" + now.getHours(); 
    if (hour.length == 1) { 
     hour = "0" + hour; 
    } 
    minute = "" + now.getMinutes(); 
    if (minute.length == 1) { 
     minute = "0" + minute; 
    } 
    day = "" + now.getDay(); 
    if (day.length == 1) { 
     day = "0" + day; 
    } 
    var Time = day + '' + hour + '' + minute; 
    if (Time >= 010835 && Time <= 010920) { 
     document.getElementById('Man1').style.background = 'green'; 
    } else { 
     document.getElementById('Man1').style.background = ''; 
    } 
    if (Time >= 010921 && Time <= 011010) { 
     document.getElementById('Man2').style.background = 'green'; 
    } else { 
     document.getElementById('Man2').style.background = ''; 
    } 
    if (Time >= 011011 && Time <= 011105) { 
     document.getElementById('Man3').style.background = 'green'; 
    } else { 
     document.getElementById('Man3').style.background = ''; 
    } 
    if (Time >= 011106 && Time <= 011155) { 
     document.getElementById('Man4').style.background = 'green'; 
    } else { 
     document.getElementById('Man4').style.background = ''; 
    } 
    if (Time >= 011156 && Time <= 011239) { 
     document.getElementById('Man5').style.background = 'green'; 
    } else { 
     document.getElementById('Man5').style.background = ''; 
    } 
    if (Time >= 011240 && Time <= 011325) { 
     document.getElementById('Man6').style.background = 'green'; 
    } else { 
     document.getElementById('Man6').style.background = ''; 
    } 
    if (Time >= 011326 && Time <= 011415) { 
     document.getElementById('Man7').style.background = 'green'; 
    } else { 
     document.getElementById('Man7').style.background = ''; 
    } 
    if (Time >= 011416 && Time <= 011505) { 
     document.getElementById('Man8').style.background = 'green'; 
    } else { 
     document.getElementById('Man8').style.background = ''; 
    } 
    if (Time >= 011506 && Time <= 011555) { 
     document.getElementById('Man9').style.background = 'green'; 
    } else { 
     document.getElementById('Man9').style.background = ''; 
    } 
} 
setInterval(one, 1000); 

Fiddle

+1

計算機不是人類。你不能僅僅忽略那些似乎暗示給你的東西,並且期望他們只是像人類一樣去理解它。 – lanzz

回答

3

時間是一個字符串,你是比較爲int。將值放在引號中:

if (Time>='020000' && Time<='030000'){ 
+0

在jsfiddle工作。 我目前有麻煩上傳文件到我的網絡主機,所以只要我在線測試它將此設置爲Answare。 – SyNpRiX

相關問題