gpt4 book ai didi

javascript - 如何将时间戳转换为 UTCString 并防止浏览器将日期更改为本地时区?

转载 作者:行者123 更新时间:2023-11-30 13:54:09 24 4
gpt4 key购买 nike

我有一个充满时间戳的数组,例如:

let tAxe = [1548546272000, 1548546287000, 1548546303000, 1548546318000, 1548546333000];

我正在尝试将每个时间戳转换为人类可读的日期。我可以阻止浏览器将此日期转换为当前时区吗?此 tAxe 变量在 amChart4 库中用于生成图形。

我在想一个循环,但我不确定如何准确地编写代码

for (i = 0; i < tAxe.length; i++) { 
// I don't know how to write exactly the conversion

}

最佳答案

您只需将 dateFormatter.utc 设置为 true,amCharts 将为您完成剩下的工作:

chart.dateFormatter.utc = true;

这是一个使用您分享的时间戳的示例:

am4core.useTheme(am4themes_animated);

var chart = am4core.create("chartdiv", am4charts.XYChart);

chart.dateFormatter.utc = true;

chart.data = [
{date:1548546272000, value: 12},
{date:1548546287000, value: 15},
{date:1548546303000, value: 13},
{date:1548546318000, value: 17},
{date:1548546333000, value: 14},
];

var dateAxis = chart.xAxes.push(new am4charts.DateAxis());

var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());

var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.dateX = "date";
#chartdiv {
width: 100%;
height: 500px;
}
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>

Here是一个代码笔,也显示了示例。

关于javascript - 如何将时间戳转换为 UTCString 并防止浏览器将日期更改为本地时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57625056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com