gpt4 book ai didi

javascript - 获取日期数组的时间戳值

转载 作者:行者123 更新时间:2023-12-02 22:10:28 25 4
gpt4 key购买 nike

我有一个日期数组,其中的日期时间值一直到微秒。

所以我的问题是如何将这些值转换为时间戳值,例如整数或 float 的形式如下类型:

1578032412798

这里我有如下日期数组:

var dates =["2020-14-03 11:14:48.225000","2020-14-03 11:14:48.225000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.232000"] ;

我正在尝试以下代码。但它不起作用:

 dates.forEach((e) => {
var date = e.getTime();
console.log (date)});

我还尝试实现以下 mwtod。但它只显示几个小时之前的值:

var dateString = dates,
dateTimeParts = dateString.split(' '),
timeParts = dateTimeParts[1].split(':'),
dateParts = dateTimeParts[0].split('-'),
date;
x = new Date(dateParts[2], parseInt(dateParts[1], 10) - 1, dateParts[0], timeParts[0], timeParts[1]);

mainval = x.getTime();

最佳答案

您可以使用正则表达式提取日期的年、月和日部分,然后根据重构的字符串以正确的顺序创建新的日期:

var dates = [
"2020-14-03 11:14:48.225000",
"2020-14-03 11:14:48.225000",
"2020-14-03 11:14:48.226000",
"2020-14-03 11:14:48.226000",
"2020-14-03 11:14:48.227000",
"2020-14-03 11:14:48.227000",
"2020-14-03 11:14:48.228000",
"2020-14-03 11:14:48.228000",
"2020-14-03 11:14:48.228000",
"2020-14-03 11:14:48.229000",
"2020-14-03 11:14:48.229000",
"2020-14-03 11:14:48.229000",
"2020-14-03 11:14:48.230000",
"2020-14-03 11:14:48.230000",
"2020-14-03 11:14:48.230000",
"2020-14-03 11:14:48.231000",
"2020-14-03 11:14:48.231000",
"2020-14-03 11:14:48.231000",
"2020-14-03 11:14:48.231000",
"2020-14-03 11:14:48.232000"
];
res = dates.map(d => {
m = d.match(/^(\d+)-(\d+)-(\d+) (.*)$/);
return new Date(`${m[1]}-${m[3]}-${m[2]} ${m[4]}`).getTime();
});
console.log(res);

关于javascript - 获取日期数组的时间戳值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59574070/

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