How to format UNIX timestamp to datetime in JavaScript from API?

Viewed 24

I am trying to convert UNIX time from this API into datetime to show on a chart, how could I do that? Here is the code I have:

import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
import { Line } from '@ant-design/plots';
import { Card } from "antd";
import { DateTime } from 'luxon';

function App() {
  const [data, setData] = useState([]);
  useEffect(() => {
    asyncFetch();
  }, []);

  const asyncFetch = () => {
    fetch('https://api.llama.fi/charts/Ethereum')
      .then((response) => response.json())
      .then((json) => setData(json))
      .catch((error) => {
        console.log('fetch data failed', error);
      });
  };

  const config = {
    data,
    padding: 'auto',
    xField: 'date',
    yField: 'totalLiquidityUSD'

Here is picture of the chart:

chart

1 Answers

multiply seconds by 1000 for ms since 1970. Then use setTime method of Date object.

var ts = 1663286400
var d = new Date()
d.setTime(ts * 1000)
console.log(d)

Related