如何确定是白天还是晚上在Javascript中? [英] How to determine if it is day or night in Javascript?

查看:59
本文介绍了如何确定是白天还是晚上在Javascript中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据浏览器的时间将不同的CSS工作表应用于我的网站.例如,如果是白天,则将day.css或night.css显示为夜晚.

Im wanting to apply different CSS sheets to my site depending on the time of the browser. e.g if its day time, display day.css or night.css for night.

我可以使用PHP来做到这一点,但是它基于服务器时间,而不是浏览器的本地时间.

I can do this with PHP but it is based on the server time, not the browsers local time.

有没有办法用javascript告诉时间?一旦解决了问题,我可能会使用jQuery来简单地启动css.

Is there a way to tell the time in javascript? I'm probably going to use jQuery to simply initiate the css once its worked out.

推荐答案

var hr = (new Date()).getHours(); //get hours of the day in 24Hr format (0-23)

根据您对白天/黑夜的定义,执行魔术:)

Depending on your definition of day/night, perform your magic :)

PS:如果您的白天/夜晚不是在确切的时间开始,则可以尝试 getMinutes().

PS: If your day/night starts not at the exact hour, you can try getMinutes().

这篇关于如何确定是白天还是晚上在Javascript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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