Elijah Ellanski
Elijah Ellanski

Reputation: 922

Weird time formating with chart.js and moment.js

Every second I'm updating my chart with new data, where label is correct unix timestamp. Here's options for my x-axis

      xAxes: [
        {
          ticks: {
            maxRotation: 0,
            minRotation: 0,
          },
          type: "time",
          time: {
            displayFormats: {
              second: "HH:mm:ss"
            }
          }
        }
      ],

I'm wondering to get from timestamp 1535577869 something like 21:24:29 but getting 9:32:57.871 pm

Upvotes: 1

Views: 316

Answers (1)

timclutton
timclutton

Reputation: 13024

A JavaScript timestamp (see Date.now()) is in milliseconds, not seconds like a Unix timestamp.

To make moment.js recognise your timestamp you probably need to multiply it by a thousand (since Chart.js is passing your value to moment.js):

1535577869 * 1000

If you were creating a moment directly you could specify an input format:

moment(1535577869, 'X');

Upvotes: 1

Related Questions