JavaScript中的时间/小时语法-如何执行非';不准时

JavaScript中的时间/小时语法-如何执行非';不准时,javascript,syntax,Javascript,Syntax,我使用jQuery显示不同的消息,这取决于我工作的业务是否开放。由于业务只在9点30分开始,我需要能够将其写入jQuery,但到目前为止,我只能通过谷歌搜索指定时间的东西 var thehours = new Date().getHours(); var themessage; var open = ('nu open'); var gesloten = ('nu gesloten'); if (thehours >= 9.30 && thehours < 18)

我使用jQuery显示不同的消息,这取决于我工作的业务是否开放。由于业务只在9点30分开始,我需要能够将其写入jQuery,但到目前为止,我只能通过谷歌搜索指定时间的东西

var thehours = new Date().getHours();
var themessage;
var open = ('nu open');
var gesloten = ('nu gesloten');

if (thehours >= 9.30 && thehours < 18) {
    themessage = open; 

} else if (thehours >= 18 && thehours < 24) {
    themessage = gesloten;

} else if (thehours >= 0 && thehours < 9.30) {
    themessage = gesloten;
}

$('.bericht').append(themessage);


var thehours1 = new Date().getHours();
var themessage1;
var open1 = ('09.30 - 18.00');
var gesloten1 = ('18.00 - 09.30');

if (thehours1 >= 9.30 && thehours1 < 18) {
    themessage1 = open1; 

} else if (thehours1 >= 18 && thehours1 < 24) {
    themessage1 = gesloten1;

} else if (thehours1 >= 0 && thehours1 < 9.30) {
    themessage1 = gesloten1;
}


$('.bericht1').append(themessage1);
var thehours=newdate().getHours();
var消息;
var open=('nu open');
var gesloten=('nu gesloten');
如果(小时数>=9.30和小时数<18){
themessage=打开;
}否则如果(小时数>=18和小时数<24){
themessage=gesloten;
}否则如果(小时数>=0和小时数<9.30){
themessage=gesloten;
}
$('.bericht').append(themessage);
var thehours1=新日期().getHours();
var themessage1;
var open1=('09.30-18.00');
var gesloten1=('18.00-09.30');
如果(小时数1>=9.30和小时数1<18){
themessage1=open1;
}否则,如果(小时数1>=18和小时数1<24){
themessage1=gesloten1;
}否则如果(小时数1>=0和小时数1<9.30){
themessage1=gesloten1;
}
$('.bericht1').append(themessage1);
这是两条不同的消息,显示“现在打开/现在关闭”以及打开或关闭时间


它可以工作,但似乎只表明我们在10.00和18.00之间开放,而不是9.30和18.00,所以我想知道9.30上的语法是否错误。

9.30
是一个浮点数(即9和十分之三),而不是代码中的小时和分钟。
由于
getHours()
返回整小时的整数,因此您的代码的行为类似于:

if (thehours1 >= 10 && thehours1 < 18) {
    themessage1 = open1; 

} else if (thehours1 >= 18 && thehours1 < 24) {
    themessage1 = gesloten1;

} else if (thehours1 >= 0 && thehours1 < 9) {
    themessage1 = gesloten1;
}

9.30
是一个浮点数(即9和十分之三),而不是代码中的小时和分钟。
由于
getHours()
返回整小时的整数,因此您的代码的行为类似于:

if (thehours1 >= 10 && thehours1 < 18) {
    themessage1 = open1; 

} else if (thehours1 >= 18 && thehours1 < 24) {
    themessage1 = gesloten1;

} else if (thehours1 >= 0 && thehours1 < 9) {
    themessage1 = gesloten1;
}
快速查看会告诉您它返回

整数,介于0和23之间,根据当地时间表示给定日期的小时数

因此,使用
9.30
永远不会起作用(如果真的起作用,那么你就用十进制数字来表示时间了——难道不是
9.5
?!)

因此,您需要几小时和几分钟:

var-date=新日期();
var hrs=date.getHours();
var mins=date.getMinutes();
如果((小时==9分钟和分钟>30)| |小时>=10){
console.log(“过去的930am”)
}
快速查看会告诉您它返回

整数,介于0和23之间,根据当地时间表示给定日期的小时数

因此,使用
9.30
永远不会起作用(如果真的起作用,那么你就用十进制数字来表示时间了——难道不是
9.5
?!)

因此,您需要几小时和几分钟:

var-date=新日期();
var hrs=date.getHours();
var mins=date.getMinutes();
如果((小时==9分钟和分钟>30)| |小时>=10){
console.log(“过去的930am”)

}
您的问题与jQuery无关,它只是一个关于JS语法的问题……但它是关于javascript;)对不起,是我的错/您的问题与jQuery无关,它只是一个关于JS语法的问题……但它是关于javascript;)对不起,是我的错/谢谢大家!!现在一切都有意义了:谢谢你们!现在一切都有意义了:D
function getMinuteOfDay(hour, minute)
{
    return hour * 60 + minute;
}

var now = new Date();
var nowMinuteOfDay = getMinuteOfDay(now.getHours(), now.getMinutes());

var isOpen = nowMinuteOfDay >= getMinuteOfDay(9, 30) && nowMinuteOfDay <= getMinuteOfDay(18, 00);
themessage1 = isOpen ? open1 : gesloten1;