javascript比较两次

时间:2017-05-04 13:52:42

标签: javascript

尝试让它在16:30:00和17:30:00之间更改,以更改文本字体颜色

尝试嵌套的if语句



function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =
    h + ":" + m + ":" + s;
    var t = setTimeout(startTime, 500);
	
	if ((h>=16 && m >=30) && (h<=17 && m<=30))
	{
	    document.getElementById("txt").style.color = "red";	
	}
	else
	{
document.getElementById("txt").style.color = "black";	
	}
}
function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;

}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

尝试使用setInterval()它看起来更清晰:

function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();

    if((h==16 && m>=30) || (h==17 && m<=30)){
        document.getElementById("txt").style.color = "red"; 
    }
    else
    {
        document.getElementById("txt").style.color = "black";   
    }
};

setInterval(startTime, 500);

答案 1 :(得分:1)

你的逻辑中的主要错误是寻找小于等于30且大于等于30的分钟。部分评估为真的唯一方法是分钟是否为30。

我会将时间标准化(以便小时,分钟和秒的每个组合可以用唯一的数字表示)并将其用于比较。

&#13;
&#13;
function startTime(el) {
  var today = new Date();
  var hour = today.getHours(),
    minute = today.getMinutes(),
    second = today.getSeconds();
  var normalized_time = normalizeTime(hour, minute, second);

  document.getElementById(el).innerHTML 
    = hour + ":" + padTime(minute) + ":" + padTime(second);

  document.getElementById(el).style.color 
    = normalized_time >= normalizeTime(16, 30, 0) 
      && normalized_time <= normalizeTime(17, 30, 0)
    ? 'red'
    : 'black';
}

/*
 * Add a 0 to the beginning of the number if one-digit number
 */
function padTime(i) {
  return i < 10 ? '0' + i : i;
}

/*
 * Converts the time to a normalized version
 */
function normalizeTime(h, m, s) {
  return s + (60 * m) + (60 * 60 * h);
}

var t = setInterval(startTime, 500, 'txt');
&#13;
<span id="txt"></span>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

修改:根据您的问题,您需要将小时和分钟考虑进行比较,所以我没有包含第二部分。

以下函数获取一个随时间变化的整数值,稍后用于比较。

function getSeconds(hh,mm,ss)
{
    return (Number(hh) * 60 * 60) + (Number(mm) * 60) + Number(ss);
}

完整代码:

function getSeconds(hh,mm,ss)
{
    return (Number(hh) * 60 * 60) + (Number(mm) * 60) + Number(ss);
}
function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =
    h + ":" + m + ":" + s;
    var t = setTimeout(startTime, 500);
      var timeNow = getSeconds(h,m,s);
    //if ((h>=16 && m >=30) && (h<=17 && m<=30))
  if (timeNow >= getSeconds(16,30,0) && timeNow <= getSeconds(17,30,00))
    {
        document.getElementById("txt").style.color = "red"; 
    }
    else
    {
document.getElementById("txt").style.color = "black";   
    }
}
function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;

}
startTime();

更新了Fiddle

以前的回复

尝试Fiddle

您的代码存在的问题是您将m与矛盾的条件进行比较。

我已将小时和分钟连接起来,并将其用于时间比较

var hrs = Number(h+'.'+m);

function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =
    h + ":" + m + ":" + s;
    var t = setTimeout(startTime, 500);
    var hrs = Number(h+'.'+m);
    //if ((h>=16 && m >=30) && (h<=17 && m<=30))
    if (hrs >= 16.30 && hrs <= 17.30)
    {
        document.getElementById("txt").style.color = "red"; 
    }
    else
    {
        document.getElementById("txt").style.color = "black";   
    }
}
function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;

}
startTime();