每天根据日期和时间更改文本 [英] change text daily based on day and time of day

查看:87
本文介绍了每天根据日期和时间更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm pretty new to JavaScript but am trying to have a text change according to the time of day, and display different text based on the day.

目前,我很难搞清楚每天如何显示不同的文字。例如从周一到周四,而不是说接近,它会说我们在下午6点开放,并且在周五和周五开放。周六,它会说我们在上午11点开放。

currently, I'm stuck figuring out how to display a different text each day. For example from Mon to Thursday instead of saying close, it will say we are open at 6pm, and on Fri & Sat, it will say we are open at 11am.

有什么想法吗?谢谢

HTML:

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

JavaScript:

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'
    );


推荐答案

尝试这个

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());
});

这篇关于每天根据日期和时间更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆