从 DarkSky API 请求返回意外的时间值

Unexpected time values returned from DarkSky API request

我正在使用 DarkSky 天气 API 制作 Forecast 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,
          },...

返回的数据似乎有道理。返回了 49 个每小时预报项目,每个项目中的天气统计数据似乎反映了每小时变化的情况。我希望每个小时预报对象中的 "time" 值是不同的时间,涵盖 48 小时,但事实并非如此。

当我像这样映射并格式化预测中的时间时

const hourlyForecast = data.hourly.data.map(forecastData =>
moment(forecastData.time).format("h:mm a"));

console.log(hourlyForecast);

日志

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",
]

为什么时间戳不是单独的时间?为什么他们只报道 4 分钟而不是 48 小时?这些时间对我来说没有意义。感谢您的帮助!

它返回秒而不是毫秒。乘以 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)

所以改为这样做:

moment(forecastData.time * 1000).format("h:mm a"));