I'm making a Forecast Request using DarkSky weather API and I do not understand the logic of the time values returned.
The request
"https://api.darksky.net/forecast/[secretkey]/42.3601,-71.0589?exclude=alerts,flags,currently,minutely,daily"
returns
Object {
"hourly": Object {
"data": Array [
Object {
"apparentTemperature": 41.32,
"cloudCover": 0.56,
"dewPoint": 33.99,
"humidity": 0.58,
"icon": "rain",
"ozone": 361.4,
"precipIntensity": 0.0026,
"precipProbability": 0.31,
"precipType": "rain",
"pressure": 992.3,
"summary": "Possible Drizzle",
"temperature": 47.88,
"time": 1582826400,
"uvIndex": 2,
"visibility": 10,
"windBearing": 265,
"windGust": 34.53,
"windSpeed": 17.61,
},
Object {
"apparentTemperature": 38.77,
"cloudCover": 0.62,
"dewPoint": 31.49,
"humidity": 0.57,
"icon": "partly-cloudy-day",
"ozone": 363.6,
"precipIntensity": 0,
"precipProbability": 0,
"pressure": 993.7,
"summary": "Mostly Cloudy",
"temperature": 45.9,
"time": 1582830000,
"uvIndex": 2,
"visibility": 10,
"windBearing": 263,
"windGust": 33.82,
"windSpeed": 17.51,
},
Object {
"apparentTemperature": 34.63,
"cloudCover": 0.52,
"dewPoint": 29.49,
"humidity": 0.59,
"icon": "partly-cloudy-day",
"ozone": 364.4,
"precipIntensity": 0,
"precipProbability": 0,
"pressure": 995.1,
"summary": "Partly Cloudy",
"temperature": 42.87,
"time": 1582833600,
"uvIndex": 1,
"visibility": 10,
"windBearing": 274,
"windGust": 34.63,
"windSpeed": 18.34,
},...
The data returned seems to make sense. There are 49 hourly forecast items returned, the weather stats in each seem to reflect conditions changing hourly. I would expect that the "time" value in each hourly forecast object would be for a different hour covering 48 hours, but that is not the case.
When I map through and format the times in the forecast like this
const hourlyForecast = data.hourly.data.map(forecastData =>
moment(forecastData.time).format("h:mm a"));
console.log(hourlyForecast);
logs
Array [
"11:40 pm",
"11:40 pm",
"11:40 pm",
"11:40 pm",
"11:40 pm",
"11:40 pm",
"11:40 pm",
"11:40 pm",
"11:40 pm",
"11:40 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:41 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:42 pm",
"11:43 pm",
"11:43 pm",
"11:43 pm",
"11:43 pm",
"11:43 pm",
"11:43 pm",
]
Why are the timestamps not for separate hours? Why do they only cover 4 minutes and not 48 hours? These times don't make sense to me. Thanks for your help!
It's returning seconds instead of milliseconds. Multiply by 1000.
// wrong - timestamps in seconds
new Date(1582826400)
Mon Jan 19 1970 01:40:26 GMT-0600 (Central Standard Time)
new Date(1582830000)
Mon Jan 19 1970 01:40:30 GMT-0600 (Central Standard Time)
// right - timestamp in milliseconds
new Date(1582826400 * 1000)
Thu Feb 27 2020 12:00:00 GMT-0600 (Central Standard Time)
new Date(1582830000 * 1000)
Thu Feb 27 2020 13:00:00 GMT-0600 (Central Standard Time)
So do this instead:
moment(forecastData.time * 1000).format("h:mm a"));