Skip to content

Instantly share code, notes, and snippets.

@nanoDBA
Created December 22, 2022 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanoDBA/2b50ba01ad5736c4eb89040f3a2b42bc to your computer and use it in GitHub Desktop.
Save nanoDBA/2b50ba01ad5736c4eb89040f3a2b42bc to your computer and use it in GitHub Desktop.
How many minutes of daylight are there in a given day?
# Yesterday was the winter solstice, so I am looking forward to more daylight!
# https://stackoverflow.com/questions/63236774/powershell-return-only-sunrise-sunset-time/63237047#63237047
# using API from https://sunrise-sunset.org/api
$lat = 35.787743 ; $long = -78.644257 # substitute desired latitude/longitude
# date range: 2 days ago until tomorrow and Loop de Loop using foreach
-2..1 | foreach {
$Date = (Get-Date).AddDays($_) | Get-Date -Format "yyyy-MM-dd"
$Daylight = (Invoke-RestMethod "https://api.sunrise-sunset.org/json?lat=$lat&lng=$long&formatted=0&date=$Date").results
$Sunrise = ($Daylight.Sunrise | Get-Date -Format "HH:mm")
$Sunset = ($Daylight.Sunset | Get-Date -Format "HH:mm")
Write-Output "$Date Minutes Daylight: $($Daylight.day_length/60)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment