在javascript中比较两次
我需要运行一个特定的逻辑,如果当前时间用于以下情况在javascript中比较两次,javascript,time,Javascript,Time,我需要运行一个特定的逻辑,如果当前时间用于以下情况 上午7点不到 上午7点到9点之间 上午9点到晚上7点之间 下午7点以上 我发现很难看出当前时间是否介于以下时间间隔之间: if(currentHour < 7){ console.log("Time is before than 7 am :" + 0 ); } else if (currentHour > 19){ console.log("Time is after than 7 pm :" + 12148 );
if(currentHour < 7){
console.log("Time is before than 7 am :" + 0 );
}
else if (currentHour > 19){
console.log("Time is after than 7 pm :" + 12148 );
}
else if(//Compare between the hours)
if(当前小时<7){
console.log(“时间早于上午7点:+0”);
}
否则如果(当前小时>19){
控制台日志(“时间在下午7点之后:+12148”);
}
else if(//比较小时数)
请使用适当的代码进行帮助。
如果(currentHour>=7&¤tHour请尝试下一个代码。Date
类的getHours()
方法以24小时格式返回当前小时。(4pm
是16
,7pm
是19
,等等。)
if (currentHour >= 7 && currentHour <=19) {
}
var currentHour=new Date().getHours();
如果(当前小时<7){
控制台日志(“上午7点以下”);
}
如果(当前小时>=7&¤tHour=9&¤tHour 19){
控制台日志(“大于晚上7点”);
}
如果您处理的是单个时间间隔,您可以像这样使用(&&
):
if(currentHour >= 7 && currentHour <= 9) {...}
(您可能需要更改
if(currentHour >= 7 && currentHour <= 9) {...}
if(currentHour < 7) { //less than 7am
}
else if(currentHour <= 9) { //between 7am and 9am
}
else if(currentHour <= 19) { //between 9am and 7pm
}
else { //greater than 7pm
}