【发布时间】:2020-01-25 05:11:29
【问题描述】:
我从 DarkShy 天气 api 接收 JSON 对象,我想访问 Chart.JS 图表的每个报告的时间戳,我将在其中显示一天的温度,现在我被困在转换时间戳转换成 HH:DD:SS 格式。
这是我尝试过的
// Displays the wrong time according to https://www.epochconverter.com/
var timeofDay = new Date(daily[i].time)
time.push( timeofDay.toTimeString().split(' ')[0] )
// Gets rid off the time, tho It get the date correctly
var timeofDay = new Date(parseFloat(daily[i].time) * 1000)
time.push( timeofDay )
// Returns the wrong date and time
time.push(new Date(daily[i]))
以下是我循环遍历 JSON 文件的方式
let time = []
let temperatureDaily = []
for(var i=0; i<daily.length; i++){
// Push the values into the arrays
var timeofDay = new Date(parseFloat(daily[i].time) * 1000)
time.push( timeofDay )
temperatureDaily.push( (parseFloat(daily[i].temperatureHigh) + parseFloat(daily[i].temperatureLow)) /2)
}
console.log(time);
【问题讨论】:
-
daily[i].time的值是多少? -
timeofDay.getHours() 给了我错误的时间,根据 epochconverter.com 应该是早上 6 点左右。这是时间戳:1569304800,返回值为:21
-
您需要获得 UTCHours:
new Date(1569304800 * 1000).getUTCHours()是6。你得到的是基于主机时区偏移的本地时间。
标签: javascript json timestamp weather-api