如何从API用JavaScript将UNIX时间戳格式化为日期时间



我正在尝试将此API中的UNIX时间转换为日期时间以显示在图表上,如何做到这一点?这是我的代码:

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'

这是图表的图片:

图表

自1970年以来,

将秒乘以1000表示ms。然后使用Date对象的setTime方法。

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

最新更新