Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对当前时间使用JavaScript比较运算符_Javascript_Html - Fatal编程技术网

对当前时间使用JavaScript比较运算符

对当前时间使用JavaScript比较运算符,javascript,html,Javascript,Html,例如,我正在使用JavaScript制作一个HTML表,根据时间隐藏某些部分 6:30 6:45 7:05 当当前时间等于或大于6:30时,第一个单元格应隐藏 我开始的方式是 var now=新日期();//创建日期对象的步骤 var h=now.getHours();//获取当前小时数的步骤 var m=now.getMinutes();//获取当前分钟数 后来, 如果(h>=6&&m>=30){ $('table#truetable tr:first').hide(); } 这不起作用(我认

例如,我正在使用JavaScript制作一个HTML表,根据时间隐藏某些部分

6:30
6:45
7:05

当当前时间等于或大于6:30时,第一个单元格应隐藏

我开始的方式是

var now=新日期();//创建日期对象的步骤
var h=now.getHours();//获取当前小时数的步骤
var m=now.getMinutes();//获取当前分钟数

后来,

如果(h>=6&&m>=30){
$('table#truetable tr:first').hide();
}

这不起作用(我认为问题在最后一部分),因为它不会隐藏这个(第一个)单元格,比如说7:25,因为当时的分钟数不大于30,这意味着这种方法在许多其他情况下不起作用

我能修好这个吗?我需要用另一种方法吗?

按分钟比较:

if( h*60+m/*h:m*/ >= 6*60+30/*6:30*/ ){
}
按分钟比较:

if( h*60+m/*h:m*/ >= 6*60+30/*6:30*/ ){
}

最简单的方法是在6点钟单独处理案件:

if(h>6 | |(h==6&&m>=30)){
//修改DOM
}

最简单的方法是在6点钟单独处理案件:

if(h>6 | |(h==6&&m>=30)){
//修改DOM
}

我编写了一个函数,将
hh:mm
hh:mm:ss
格式中的时间转换为秒。你可以在下面找到它:

function hourConvert(str) {
    //this separates the string into an array with two parts, 
    //the first part is the hours, the second the minutes
    //possibly the third part is the seconds
    str = str.split(":"); 

    //multiply the hours and minutes respectively with 3600 and 60
    seconds = str[0] * 3600 + str[1] * 60;

    //if the there were seconds present, also add them in
    if (str.length == 3) seconds = seconds + str[2];

    return seconds;
}
现在很容易相互比较时间:

if (hourConvert(str) > hourConvert("6:30")) //Do Stuff

查看它的操作:

我编写了一个函数,将
hh:mm
hh:mm:ss
格式中的时间转换为秒。你可以在下面找到它:

function hourConvert(str) {
    //this separates the string into an array with two parts, 
    //the first part is the hours, the second the minutes
    //possibly the third part is the seconds
    str = str.split(":"); 

    //multiply the hours and minutes respectively with 3600 and 60
    seconds = str[0] * 3600 + str[1] * 60;

    //if the there were seconds present, also add them in
    if (str.length == 3) seconds = seconds + str[2];

    return seconds;
}
现在很容易相互比较时间:

if (hourConvert(str) > hourConvert("6:30")) //Do Stuff
在行动中看到它:

这确实有效。您的问题是您正在检查时间和分钟数,这意味着如果分钟数小于30,它将返回false

您的if转换为:

any hour bigger than six whose minutes are also bigger than 30
您的if条件应为:

if(h>=6 && m>=30 || h>=7)
或者只带数字

if(h*60+m>= 390)
这确实有效。您的问题是您正在检查时间和分钟数,这意味着如果分钟数小于30,它将返回false

您的if转换为:

any hour bigger than six whose minutes are also bigger than 30
您的if条件应为:

if(h>=6 && m>=30 || h>=7)
或者只带数字

if(h*60+m>= 390)

“我想”是什么意思?您是否放置了
console.log(h);console.log(m)
查看值???我看到了值,它们与当前24小时格式的时间相同,为什么?你说的“我想”是什么意思?您是否放置了
console.log(h);console.log(m)
要查看值吗?我看到了值,它们与当前时间的24小时格式相同,为什么?链接不是答案。请把答案写在这里,不要写在别的地方。谢谢。链接不是答案。请把答案写在这里,不要写在别的地方。谢谢