Search code examples
javascriptamchartsamcharts4

amCharts dateFormatter.inputDateFormat not reading Unix AKA Epoch timestamps Correctly


I need to use Unix time for my amChart source (1608713282) should be 12/23/20 04:30:00, however it seems to be reading it as 01/19/70 00:00:00 in the tooltip.

I've tried using the following lines of code to format it correctly on chart and on my dateAxis...

chart.dateFormatter.inputDateFormat = 'x';
chart.dataSource.parser.options.dateFormat = 'x';
chart.dateFormatter.utc = true;

dateAxis.dateFormatter.inputDateFormat = 'x';
dateAxis.dataSource.parser.options.dateFormat = 'x';
dateAxis.dateFormatter.utc = true;

Here is the code related to my dateAxis:

// TimeStamp Axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.minGridDistance = 100;
dateAxis.tooltipDateFormat = 'MM/dd/yy H:mm:ss';

What I found interesting thru some debug is that wrapping the timestamp value in my JSON source in quotes changes the displayed date from 01/19/70 00:00:00 to 11/16/13 23:59:58, different but still incorrect.

Here is an example of my data:

{
    "tstamp": 1608697800,
    "vol": 144,
    "staff": 61,
    "avail": 0
}

Solution

  • The 'x' format looks for Unix timestamps in milliseconds, per the docs. You're providing values in seconds, which won't parse correctly.