spacetime-daylight

1.5.1 • Public • Published
predicted sunrise/sunset times
npm install spacetime-daylight

a spacetime plugin to calculate sunlight exposure for a given timezone/location, in local time.

This project is really just a neat opportunity to combine some exceptional open-source javascript libraries:

that's all it does.

it bundles-in the spacetime-geo plugin, too.

sunrise/sunset

import spacetime from 'spacetime'
import daylight from 'spacetime-daylight'
spacetime.extend(daylight)

let d = spacetime('June 5th 3:30pm', 'Canada/Eastern')
d.sunrise().time()
// 6:43am
d.sunset().time()
// 7:13pm
d.noon().time()
// 1:17pm

132kb or so,

solar position

find the position of the sun, at a place and time:

let s = spacetime.today('Europe/Stockholm').time('3:00am')
let hours = s.every('hour', s.time('11:00pm'))
hours.forEach((d) => {
  console.log(d.time(), d.sunPosition())
})

Altitude means how high the sun is. 90° is directly overhead, 0° means it's setting, or rising. negative numbers means it's dark.

Azimuth describes how far it is east-to-west 0° is North. Negative numbers are east (morning). Noon should have the same azimuth as midnight.

solstice calculator

some rough millisecond-math for estimating solstice dates for a given year:

let s = spacetime('march 3rd 2007')
console.log(s.summerSolstice().format('nice'))

(this is not recommended for advanced astronomy, or distant years)

API

d.sunrise()

d.sunset()

d.noon()

d.dusk()

d.dawn()

//this one does some helper diff logic
d.daylight()
/*{
  dawn: '5:02am',
  sunrise: '5:38am',
  sunset: '9:04pm',
  dusk: '9:40pm',
  duration:
   { inHours: 16,
     inMinutes: 926,
     inSeconds: 55540,
     human: { hours: 15, minutes: 25, seconds: 40 } },
  current: { progress: 0.49936982355059417, status: 'day' }
}*/

Examples:

find out the rate the length-of-day is changing

let s = spacetime('November 12 2018', 'Europe/London')
let today = s.daylight().inSeconds
let tomorrow = s.add(1, 'day').daylight().inSeconds
let diff = today - tomorrow
console.log(`today is ${diff / 60} minutes longer`)
// 'today is 3.2 minutes longer'

find-out where the sun is rising now:

let maybeList = spacetime.whereIts('4:00am', '9:30am')
maybeList.forEach((tz) => {
  let d = spacetime.now(tz)
  if (d.isBetween(d.dawn(), d.sunrise())) {
    //calculate how minutes until sunrise
    let diff = d.diff(d.sunrise())
    console.log(tz + ' in ' + diff.minutes + ' mins - @ ' + d.sunrise().time())
  }
})
// Asia/Kamchatka in 25 mins - @ 6:01am
// Asia/Magadan in 4 mins - @ 4:40am
// Pacific/Midway in 19 mins - @ 5:55am

See also

  • timespace - by MapBox, using Moment - larger and more-accurate.

MIT

Readme

Keywords

Package Sidebar

Install

npm i spacetime-daylight

Weekly Downloads

103

Version

1.5.1

License

MIT

Unpacked Size

50.7 kB

Total Files

14

Last publish

Collaborators

  • spencermountain