Search code examples
datetimechart.jsmomentjsvue-chartjs

Set xAxes labels to format YYYY-MM-DD HH:mm:ss in Chart.js


I have the following Bubble chart generated in Chart.js. Note the xAxes labels are already date objects.

enter image description here

Here is the current configuration of the xAxes.

scales: {
  xAxes: [{
    type: 'time',
    time: {
      unit: 'second',
    }
  }],

How can I display the xAxes labels in format YYYY-MM-DD HH:mm:ss?


Solution

  • You can use property displayFormats according to Display Formats from Chart.js documentation. See Moment.js for the allowable format strings.

    scales: {
      xAxes: [{
        type: 'time',
        time: {
          unit: 'second',
          displayFormats: {
            second: 'YYYY-MM-DD HH:mm:ss'
          }
        }
      }]
    }