您好我正在制作一个简单的jquery函数,可以跟踪它的时间,并通过更改div的显示属性来显示商店是打开还是关闭。
但我也需要加入会议记录,因为该商店例如在7:30开放。如何使用分钟数来使其工作?
我认为它会像:&&小时+分钟< = 11.30&&小时< = 18.00)?但这不起作用。任何帮助表示赞赏
var now = new Date();
var weekday = new Array(7);
weekday[0] = "Monday";
weekday[1] = "Tuesday";
weekday[2] = "Wednesday";
weekday[3] = "Thursday";
weekday[4] = "Friday";
weekday[5] = "Saturday";
weekday[6] = "Sunday";
var checkTime = function() {
var dayOfWeek = now.getDay();
var hour = now.getHours();
var minutes = now.getMinutes();
if (( dayOfWeek == 6)) {
$('.opening-js').css('display', 'none');
$('.availability-status-closed').css('display', 'block');
}
else if ((dayOfWeek == 0 || dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3) && hour <= 11 && hour <= 18.00) {
$('.opening-js').css('display', 'none');
$('.availability-status-closed').css('display', 'block');
}
else if ((dayOfWeek == 0 || dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3) && hour >= 11 && hour <= 18.00) {
$('.opening-js').css('display', 'block');
$('.availability-status-closed').css('display', 'none');
}
else {
if ((dayOfWeek == 5) && hour >= 10.00 && hour <= 16.00) {
$('.availability-status-open').css("display", "block");
$('.availability-status-closed').css("display", "none");
}
}
};
setInterval(checkTime, 1000);
checkTime();
答案 0 :(得分:0)
代码中的hour
和minutes
都是整数
var hour = now.getHours();
var minutes = now.getMinutes();
所以你需要使用两个变量
if((hour == 7 && minutes>=30) || hour>7){
// it is 7:30 or later.
}
或者,您可以将其降低到最低公分母...... totalMinutes
var totalMinutes = (now.getHours()*60) + now.getMinutes();
var sevenThirty = (7*60)+30;
if(totalMinutes >= sevenThirty){
// it is 7:30 or later
}
答案 1 :(得分:0)
如果您想将时间作为字符串进行比较,请首先尝试将小时和分钟转换为单个字符串。
例如
var now = new Date();
var hours = now.getHours().toString().length > 1 ? now.getHours() : "0" + now.getHours();
var minutes = now.getMinutes().toString().length > 1 ? now.getMinutes() : "0" + now.getMinutes();
var time = hours + "." + minutes;
console.log(time);
另一个选项是仅比较Date
个对象。
var time1130 = new Date();
time1130.setHours(11);
time1130.setMinutes(30);
console.log(now > time1130);
或者比较整数(这可能是最简单的方法),正如Jamiec回答中提到的那样