Skip to content

Instantly share code, notes, and snippets.

@ChaseH88
Last active February 14, 2023 14:13
Show Gist options
  • Save ChaseH88/5403d811815666a4b6716a09097dd551 to your computer and use it in GitHub Desktop.
Save ChaseH88/5403d811815666a4b6716a09097dd551 to your computer and use it in GitHub Desktop.
Sunrise/sunset API that provides sunset and sunrise times for a given latitude and longitude.
// https://sunrise-sunset.org/api
let url = `https://api.sunrise-sunset.org/json?lat=${30.6502082}&lng=${-87.9074242}date=today`;
let response = await fetch(url);
let { results } = await response.json();
let final = Object.entries(results).map(([key, val]) => {
let date = new Date();
let time = new Date(new Date(`${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}-${val}`));
time.setHours(time.getHours() - date.getTimezoneOffset()/60);
return { [key]: time }
});
console.log(final);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment