0
let departDateArr=['2020-10-13 12:00','2020-10-03 13:00','2020-10-13 8:00','2020-10-16 20:00']

let arriveDateArr=['2022-12-20 00:00','2022-12-20 12:20','2022-12-19 12:20','2022-12-08 12:20']

I want to retrieve the minimum time in the departDateArr array

I want to retrieve the maximum time in the arraveDateArr array

How to get the desired time data through time comparison ?

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
  • also: [How to return the lowest date value and highest date value from an array in Javascript?](https://stackoverflow.com/questions/26735854/how-to-return-the-lowest-date-value-and-highest-date-value-from-an-array-in-java) – pilchard Dec 19 '22 at 02:43
  • If the IOS system does not support the 2020-10-13 format, how should it be converted? – 头不痛的无双 Dec 19 '22 at 02:44
  • 1
    Please update the question with the expected results. E.G. for earliest departure do you require `2020-10-13 8:00` as the earliest time of departure, or `2020-10-03 13:00` as the earliest time and date of departure. – traktor Dec 19 '22 at 02:56
  • 2020-10-03 13:00 At the earliest, I'm sorry, but my statement may not be very clear – 头不痛的无双 Dec 19 '22 at 03:05

1 Answers1

2

Your times are in a format that can be compared lexicographically, so you can simply use Array#reduce and string comparison to get the maximum or minimum.

let departDateArr=['2020-10-13 12:00','2020-10-03 13:00','2020-10-13 8:00','2020-10-16 20:00'];
let arriveDateArr=['2022-12-20 00:00','2022-12-20 12:20','2022-12-19 12:20','2022-12-08 12:20'];
let minDepart = departDateArr.reduce((a,b)=>b<a?b:a);
let maxArrive = arriveDateArr.reduce((a,b)=>b>a?b:a);
console.log(minDepart);
console.log(maxArrive);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80