Search code examples
javascriptjqueryamcharts

How to Convert date to time in amchart v3


i just want to display "6 AM" or just time only in my X AXIS, instead of showing 2019-09-05 06:00:00

Here is my sample https://jsfiddle.net/uwtz3p4h/

is there any chance that is it possible to display time only? i cant find anything in their sample in their documentation.


Solution

  • To achieve this you can use the categoryAxis.labelFunction parameter to format the labels. You can provide a function to this parameter which accepts the value of the label as a string, which you can then parse to a Date object and format as required:

    "labelFunction": function(valueText, serialDataItem, categoryAxis) {
      // '06:00' format
      var date = new Date(valueText);
      var hours = date.getHours();
      var mins = date.getMinutes();
      return ('00' + hours).slice(-2) + ':' + ('00' + mins).slice(-2);
    
      // '6AM' format:
      var hours = new Date(valueText).getHours();
      return hours + (hours >= 12 ? 'PM' : 'AM');
    }
    

    Working fiddle