`if(Date> = Date1&&< = Date2){do this}``不起作用

时间:2014-02-18 09:25:52

标签: javascript jquery

代码会将new Date()更改为DayHourMinute
例如monday9AM45minutes010945
我使用的是010945,我的代码指定了 如果var在>=010921 && <=011010之间 将background更改为绿色else

但没有任何反应,如果我使用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 个答案:

答案 0 :(得分:3)

时间是一个字符串,你正在与一个int进行比较。将值放在引号中:

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