First, you'll need to download the plugin and add it to your <head> element:
<script src="daylight.js" type="text/javascript"></script>
The current release offers two convenience methods: day() and night(), each of which will return either true or false, depending on whether it's day or night on the user's end.
For instance, night() can be used to serve a "dark" stylesheet to nighttime visitors. Here's an example of that from the header of this page:
<script type="text/javascript">
if (night()) {
document.write('<link rel="stylesheet" href="night.css" type="text/css" media="screen" />');
}
</script>
Or you can use these convenience methods to swap some images within your <body> element. (Use this one sparingly. It adds a lot of extra markup to your source.)
<script type="text/javascript">
if (day()) {
document.write('<img src="/images/logo.png" alt="logo" />');
} else {
document.write('<img src="/images/logo-night.png" alt="logo-night" />');
}
</script>
Daylight.js calculates the sunrise and sunset in terms of Coordinated Universal Time (UTC). Then it detects the difference, in hours, between your local time and UTC. Then it adds it all together.
Because it thinks in 1-hour chunks, Daylight.js is only capable of calculating one sunrise/sunset time for each time zone. And it knows nothing about a user's latitude or longitude. All of which means that its definition of "night" won't necessarily agree with what some users see when looking out their windows.
But if you think of "night" as that period of the day when people are clocking out, eating dinner, watching prime-time television and sleeping, the results are definitely in the ballpark.
Daylight.js was designed to be fast and lightweight, so it has no external dependencies. It doesn't even require an Internet connection! As a result, the only thing it knows about your location is your current time zone.
Daylight.js is perfect for adding the sort of non-critical visual polish to your site that will only be noticed by a tiny group of very observant people. Rely on it for anything else at your own peril.