Janessa Bautista
Janessa Bautista

Reputation: 294

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.

Upvotes: 1

Views: 89

Answers (2)

Negi Rox
Negi Rox

Reputation: 3932

Here i added one function

function converttoTime(t){
   var d=new Date(t);
   return d.getHours() +":"+d.getMinutes()+d.getSeconds()
}

I have modified your fiddle you can look into it here is working example

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337713

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

Upvotes: 1

Related Questions