gpt4 book ai didi

javascript - 每天根据日期和时间更改文本

转载 作者:行者123 更新时间:2023-12-03 09:29:40 25 4
gpt4 key购买 nike

我对 JavaScript 还很陌生,但我正在尝试根据一天中的时间更改文本,并根据一天显示不同的文本。

目前,我每天都在思考如何显示不同的文本。例如,从周一到周四,它不会说“关门”,而是说我们在下午 6 点营业,而在周五和周六,它会说我们在上午 11 点营业。

有什么想法吗?谢谢

HTML:

<div class="time"><a href="#contact">
<img id="clock" src="assets/clock.png">
<div id="open_close"></div></a>
</div>

JavaScript:

var data = [
{},
{open:18, close:22},
{open:18, close:22},
{open:18, close:22},
{open:12, close:22},
{open:12, close:22},
{open:12, close:22}
];

var date = new Date();
var dayOfWeek = date.getDay(); // 0 is Sunday, 1 is Monday, etc...
var openingTimes = data[dayOfWeek];
var openClosed = false; // closed by default

// check that there are opening times for today
if (openingTimes.hasOwnProperty('open') && openingTimes.hasOwnProperty('close')){
var hour = date.getHours()
openClosed = openingTimes.open <= hour && hour < openingTimes.close;
}

$("#open_close").html(
if (openClosed == True){
console.log("we are open now")
}

if else (
0 = 'we are open from mon to Mon to Mon-Sat':
1 = '6pm':
2 = '6pm':
3 = '6pm':
4 = '12pm':
5 = '12pm':
6 = '12pm'
);

最佳答案

尝试this

var data = [
{},
{open:18, close:22},
{open:18, close:22},
{open:18, close:22},
{open:12, close:22},
{open:12, close:22},
{open:12, close:22}
];

var date = new Date();
var dayOfWeek = date.getDay(); // 0 is Sunday, 1 is Monday, etc...
var openingTimes = data[dayOfWeek];
var openClosed = false; // closed by default

// check that there are opening times for today
if (openingTimes.hasOwnProperty('open') && openingTimes.hasOwnProperty('close')){
var hour = date.getHours()
openClosed = openingTimes.open <= hour && hour < openingTimes.close;
}
var msg = function () {
if (openClosed == true){
return "we are open now";
} else {
switch(dayOfWeek) {
case 1:
return "open at 6pm";
break;
case 2:
return "open at 6pm";
break;
case 3:
return "open at 6pm";
break;
case 4:
return "open at 12pm";
break;
case 5:
return "open at 12pm";
break;
case 6:
return "open at 12pm";
break;

default:
return "close";
}
}
$( document ).ready(function() {
$("#open_close").html(msg());
});

关于javascript - 每天根据日期和时间更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31556085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com