Skip to content

romanyanke/daynight

Repository files navigation

🌞 Day or night 🌚

This script tries to guess is it dark or light now in the user's location. It doesn't ask for user location. It doesn't rely on IP addresses. It doesn't use bleeding-edge features (like DeviceLightEvent or light-level CSS media).

The script gets the timezone name. It finds the coordinates of the center of this timezone and uses this information to calculate sunrise and sunset time.

The result is more or less adequate, usually. See demo here.

Browser compatibility

To get the timezone name, it uses Internationalization API https://caniuse.com/#feat=internationalization.

Installation

npm install daynight --save-dev

Usage

import daynight from 'daynight'

const isLight = daynight().light

You can pass the time zone name and/or date as options. By default, it's the user's time zone and current date.

daynight({
  timeZone: 'Africa/Nairobi',
  date: new Date('2012-12-20T12:00'),
})

The script fails when:

  • Browser doesn't support Internationalization API
  • Timezone is not found in the list of timezones.

Result

{
  coordinates: [number, number]
  dark: boolean
  light: boolean
  sunrise: Date
  sunset: Date
  timezone: string
  brightness: number
  theme: 'day' | 'night'
}

Brightness

Brightness is a value between 0 and 1. Zero is the darkest value, and 1 is the most bright one. The brightness is 0.5 at sunrise and sunset.

Changelog

See Changelog.md.

About

It is a simple way to detect whether it's light or dark in the current user location. Check the sunset time and apply a dark theme.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published