gpt4 book ai didi

javascript - 从日期戳数组中获取唯一日期

转载 作者:行者123 更新时间:2023-12-01 00:48:34 25 4
gpt4 key购买 nike

我有一组日期戳:

let dateList = [ "2019-07-22T08:40:33.537Z", "2019-07-22T08:40:33.537Z", "2019-07-23T12:02:53.143Z", "2019-07-23T12:02:53.143Z", "2019-07-23T12:12:00.434Z", "2019-07-23T12:12:54.649Z", "2019-07-23T12:14:57.547Z", "2019-07-23T12:14:57.547Z", "2019-07-23T12:16:45.011Z", "2019-07-23T12:16:45.011Z", "2019-07-23T12:21:20.527Z", "2019-07-23T12:24:30.095Z", "2019-07-23T12:25:33.251Z", "2019-07-23T12:27:53.809Z", "2019-07-23T12:27:53.809Z" ]

我需要从中获取唯一日期的列表。以下函数允许我删除重复项。

let uniqueDates = [...new Set(dateList)];

但是,它仍然是一组唯一的时间戳。我真正需要的是一系列独特的日期。因此,如果同一日期有两个时间戳,我只需要唯一数组中的一个。你能帮助我如何实现这一目标吗?

最佳答案

映射dateList以仅包含日期(删除时间部分):

let dateList = [ "2019-07-22T08:40:33.537Z", "2019-07-22T08:40:33.537Z", "2019-07-23T12:02:53.143Z", "2019-07-23T12:02:53.143Z", "2019-07-23T12:12:00.434Z", "2019-07-23T12:12:54.649Z", "2019-07-23T12:14:57.547Z", "2019-07-23T12:14:57.547Z", "2019-07-23T12:16:45.011Z", "2019-07-23T12:16:45.011Z", "2019-07-23T12:21:20.527Z", "2019-07-23T12:24:30.095Z", "2019-07-23T12:25:33.251Z", "2019-07-23T12:27:53.809Z", "2019-07-23T12:27:53.809Z" ]

const uniqueDates = [...new Set(
dateList
.map((str) => str.match(/^\d{4}-\d{2}-\d{2}/)[0])
)];
console.log(uniqueDates);

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

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