简体   繁体   English

从一系列日期戳中获取唯一的日期

[英]Get Unique Dates from an Array of Date Stamps

I have an array of date stamps: 我有一系列日期戳:

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" ]

And I need to get the list of unique dates from it. 我需要从中获取唯一日期列表。 The following function allows me to remove duplicates. 以下功能允许我删除重复项。

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

However, it's still an array of unique time stamps. 但是,它仍然是一系列独特的时间戳。 What I really need is an array of unique dates. 我真正需要的是一系列独特的日期。 So, if there are two timestamps of the same date, I need only one in the unique array. 因此,如果有两个相同日期的时间戳,则在唯一数组中只需要一个。 Can you please help how I can achieve that? 你能帮我实现这个目标吗?

Map the dateList to contain only the dates (removing the time portion): 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM