如果时间是晚上7点到早上7点之间的Javascript吗? [英] Javascript if time is between 7pm and 7am do this?

查看:204
本文介绍了如果时间是晚上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天全站免登陆