简体   繁体   English

JavaScript时间在08:30-20:00之间

[英]Javascript time between 08:30 - 20:00

I am trying to make a message show between certain time ranges in a day but i cant make it work it either shows the first IF or doesnt show anything at all with an error i cant seem to figure out. 我试图在一天中的特定时间范围之间显示一条消息,但我无法使其工作,要么显示第一个IF,要么根本不显示任何内容,但我似乎无法弄清楚。 what am i doing wrong? 我究竟做错了什么?

var today = new Date();
var hour = today.getHours();
var minute = today.getMinutes();

if(today.getDay() == 4){
        if(hour > 8 && minute > 30 || hour < 20){
            document.getElementById('test').innerHTML = ('come today till 20:00');
        } else if (hour > 20 && hour < 0){
            document.getElementById('test').innerHTML = ('Come tomorrow till 20:00');
        } else (hour > 0 && hour < 8).document.getElementById('test').innerHTML = ('Come today from 08:00 till 20:00');
    }

figured it out thanks for the help guys :) this is how it works now. 弄清楚了,谢谢大家的帮助:)这就是现在的工作方式。

        if(today.getDay() == 4){
        if((hour === 8 && minute > 30 || hour > 8) && hour < 20){
            document.getElementById('test').innerHTML = ('Kom vandaag langs in onze showtuin tot 20:00 uur donderdag');
        } else if (hour >= 20 && hour < 24){
            document.getElementById('test').innerHTML = ('Kom morgen langs in onze showtuin tot 20:00 uur');
        } else{
            document.getElementById('test').innerHTML = ('Kom vandaag langs in onze showtuin van 08:00 tot 20:00 donderdag');
        }
    }

You could simplify the conditions a bit, with checking from small values to greater values, like 您可以通过从较小值到较大值的检查来稍微简化条件,例如

if (today.getDay() == 4) {
    if (hour < 8) {
        document.getElementById('test').innerHTML = 'Come today from 08:00 till 20:00';
    } else if (hour < 20) {
        document.getElementById('test').innerHTML = 'Come today till 20:00';
    } else if (hour < 24) {
        document.getElementById('test').innerHTML = 'Come tomorrow till 20:00';
    }
}

Working with absolute minute may become simplier: 使用绝对分钟可能会变得更简单:

var today = new Date();
var crtminut = ((today/60000).toFixed(0)-today.getTimezoneOffset())%1440;
var minmin = 8*60+30;
var minmax = 20*60;

if (today.getDay() == 4) {
    if ((minmin <= crtminut) && (crtminut < minmax)) {
        ... inner period
    } else {
        ... outer period
    }
}

Each if can use the previous condition to its advantage which means if you correctly sort the conditions you can make it really simple: 每个if都可以利用以前的条件来发挥其优势,这意味着,如果您正确地对条件进行了排序,则可以使其变得非常简单:

if (hour >= 20) {
    //20:00 - 23:59
}
else if (hour > 8) {
    //9:00 - 19:59
}
else if (hour == 8 && minute >= 30) {
    //8:30 - 8:59
}
else {
    //0:00 - 8:29
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将时间00:30:20格式转换为字符串 - Convert time 00:30:20 format to string 隐藏格林尼治标准时间20:00至08:00的HTML元素 - Hide HTML element from 20:00 to 08:00 GMT 如何拥有这种格式“2020-08-08T03:20:14.362+00:00” - How to have this format “2020-08-08T03:20:14.362+00:00” 将这样的日期格式转换为 2020 年 4 月 20 日星期五 00:00:00 GMT+0530(印度标准时间)到 Javascript 中的 2020-04-20T00:00:00.000Z - convert Date format like this Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time) to 2020-04-20T00:00:00.000Z in Javascript Javascript检查时间是否在8:30 am和6:30 pm之间 - Javascript check if the time is between 8:30am and 6:30pm 为什么此Javascript在FF3.6中有效? 新日期(“ 2010-06-09T19:20:30 + 01:00”); - Why does this Javascript work in FF3.6? new Date(“2010-06-09T19:20:30+01:00”); JavaScript中的日期格式,时间为00:00:00 - Date format in javascript with time 00:00:00 Javascript函数,允许在单击时在文本框中输入范围(08-15)之间的默认时间,或默认(范围内)时间 - Javascript function to allow time input between range(08 - 15), or a default (in range) time in textbox on click 无法在 Javascript 中使用 setHours() 将时间设置为 00:00:00:00 - Unable to set the time to 00:00:00:00 using setHours() in Javascript javascript 明天的日期和 00:00 时间 - javascript tomorrow's date and 00:00 time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM