0

I am using chart.js to display "price" data over the past 30 days. I am using a price API to do so. However the .time parameter returns the time as a UNIX timestamp. I would like the time to be displayed as a readable date.

To get the time and prices:

const data = json.Data.Data
const times = data.map(obj => obj.time)
const prices = data.map(obj => obj.high)
return {
    times,
    prices
}

My chart.js labels:

labels: times,

Is there a way to return this data as a full date instead of a timestamp ?

seabass118
  • 21
  • 3
  • Yes this is possible, you can take a look in the documentation for how to do it: https://www.chartjs.org/docs/latest/axes/cartesian/time.html – LeeLenalee Mar 07 '21 at 14:56
  • I get this error when I try to change the time display format: chart.js@2.8.0:7 Uncaught TypeError: Cannot read property 'skip' of undefined – seabass118 Mar 07 '21 at 15:00

1 Answers1

-1

So I found a solution to this and it was to use moment.js. link to reference: https://www.chartjs.org/docs/latest/axes/cartesian/time.html

Solution:

(`MY API LINK=${moment(new Date()).subtract(1, 'month').format('YYYY-MM-DD')}&end=${moment(new Date()).format('YYYY-MM-DD')}`)
seabass118
  • 21
  • 3
  • Thats a *very* innefficient way to convert values and the maths looks decidedly odd - consider `new Date(unixtimestamp*1000);` – symcbean Mar 07 '21 at 15:17