admin管理员组文章数量:1244331
i use an API to retrieve weather data from Brightsky. The data itself is displayed correctly. Only the time always starts at January 1st 00:00:00:000. How can I get the time displayed correctly?
example: jsfiddle
I have already tried to adapt the code with
const categories = weatherData.map(entry => new Date(entry.timestamp + 'Z').getTime());
without success
i use an API to retrieve weather data from Brightsky. The data itself is displayed correctly. Only the time always starts at January 1st 00:00:00:000. How can I get the time displayed correctly?
example: jsfiddle
I have already tried to adapt the code with
const categories = weatherData.map(entry => new Date(entry.timestamp + 'Z').getTime());
without success
2 Answers
Reset to default 1You can (and most likely should) stick to datetime xAxis type, just add proper timestamp data to your series data like so:
const weatherData = data.weather;
const temperatureData = weatherData.map(entry => ({
x: new Date(entry.timestamp).getTime(),
y: entry.temperature
}));
const pressureData = weatherData.map(entry => ({
x: new Date(entry.timestamp).getTime(),
y: entry.pressure_msl
}));
You can further adjust the xAxis labels display based on the API: https://api.highcharts/highcharts/xAxis.dateTimeLabelFormats
Here is your demo working well: https://jsfiddle/BlackLabel/c62uy7gz/
Kind regards,
add line: categories: categories,
in xAxis
in your JSfiddle like this:
...
xAxis: {
categories: categories, // <- add this line
type: 'datetime',
labels: {
rotation: -45,
formatter: function () {
return new Date(this.value).toLocaleString('de-DE', {
weekday: 'short',
hour: '2-digit',
minute: '2-digit',
day: '2-digit',
month: '2-digit',
year: 'numeric'
});
}
}
},
...
and here is a result (i also changed some date formating just to be sure that all workong correctly):
also according to Date.prototype.toLocaleString() MDN docs:
Every time toLocaleString is called, it has to perform a search in a big database of localization strings, which is potentially inefficient. When the method is called many times with the same arguments, it is better to create a
Intl.DateTimeFormat
object and use itsformat()
method, because a DateTimeFormat object remembers the arguments passed to it and may decide to cache a slice of the database, so future format calls can search for localization strings within a more constrained context.
so if you care about speed you need to follow recommendations
本文标签: Javascript don39t display the correct time when calling the API in highchartsStack Overflow
版权声明:本文标题:Javascript don't display the correct time when calling the API in highcharts - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740202659a2240498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
const categories
– Bravo Commented Feb 16 at 21:57