如果时间在晚上 7 点到早上 7 点之间,Javascript 会这样做吗? [英] Javascript if time is between 7pm and 7am do this?

查看:22
本文介绍了如果时间在晚上 7 点到早上 7 点之间,Javascript 会这样做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 javascript 文件来检查当前时间是否在晚上 7 点到早上 7 点之间.如果是这样,它应该将我网站上的背景颜色更改为 X.如果当前时间不在晚上 7 点到早上 7 点之间,则背景颜色应为 Y.由于我是 Javascript 的新手,所以我并不了解所有内容,这就是我需要您的帮助的原因!

I want to have a javascript file which checks if current time is between 7pm and 7am. If so it should change the background color on my website to X. If the current time is not between 7pm and 7am the background color should be Y. Since I am new to Javascript I do not know everything, and that's why I need your help!

推荐答案

var today = new Date().getHours();
if (today >= 7 && today <= 19) {
   document.body.style.background = "Red";
} else {
    document.body.style.background = "Blue";
}

参见小提琴.

这篇关于如果时间在晚上 7 点到早上 7 点之间,Javascript 会这样做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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