gggaute
gggaute

Reputation: 1

How can i find the oldest date in an array of Date objects in JavaScript?

I have an array of strings corresponding to Date objects. like this:

const timestamps =  [
  '2023-03-15T10:47:38.878Z',
  '2023-03-15T10:46:51.375Z',
  '2023-03-15T10:46:39.645Z',
  '2023-03-15T10:47:19.072Z',
  '2023-03-15T10:46:20.395Z'
]

var convertedTimestamps = [];
for (var time in timestamps) {
  var timeAsDate = Date.parse(time);
  console.log(time, timeAsDate);
  convertedTimestamps.push(timeAsDate);
}
const min = Math.min(...convertedTimestamps);

They are Date objects parsed into strings for other purposes This can be any length really, this is just one instance of it. I want to find the oldest of the timestamps.

In the example above, these are the converted timestamps, and the min value is correct based on the numbers, but not the date objects. The top one is the one that is removed

946681200000
978303600000
980982000000
983401200000
986076000000
min: 946681200000
'2023-03-15T10:47:38.878Z'

It might just be a math problem, but the way i understand it, a string parsed like this returns an int representing how much time has passed since 1970 etc, and so the oldest should be closest to that time should it not? this min value is in fact the most recent of the timestamps. When i try the max value it is not correct either.

Upvotes: 0

Views: 803

Answers (4)

rjax
rjax

Reputation: 49

for..in is used to iterate over the properties of an object, not an array. If you add console.log(time) in your loop you will see that time is a string representing each index of the array. If you change to for..of your code will work.

const timestamps = [
  '2023-03-15T10:47:38.878Z',
  '2023-03-15T10:46:51.375Z',
  '2023-03-15T10:46:39.645Z',
  '2023-03-15T10:47:19.072Z',
  '2023-03-15T10:46:20.395Z'
]

for (var time in timestamps) {
  console.log(time);
}

for (var time of timestamps) {
  console.log(time);
}

Also, unless you need the convert timestamps for something else, you might want to simply get the minimum in your loop.

const timestamps = [
  '2023-03-15T10:47:38.878Z',
  '2023-03-15T10:46:51.375Z',
  '2023-03-15T10:46:39.645Z',
  '2023-03-15T10:47:19.072Z',
  '2023-03-15T10:46:20.395Z'
];

let dateTime;
let minDateTime = Date.parse(timestamps[0]);
for (var time of timestamps) {
  dateTime = Date.parse(time);
  if (dateTime < minDateTime)
    minDateTime = dateTime;
}

console.log((new Date(minDateTime)).toISOString());

Upvotes: 0

0stone0
0stone0

Reputation: 43884

Since you're dealing with ISO 8601 timestamps, you can compare any string against it

  1. Set lowest to a, an high value string
  2. Loop over timestamps
  3. If the current on is lower, overwrite
  4. Log

const timestamps = [
  '2023-03-15T10:47:38.878Z',
  '2023-03-15T10:46:51.375Z',
  '2023-03-15T10:46:39.645Z',
  '2023-03-15T10:47:19.072Z',
  '2023-03-15T10:46:20.395Z'
];
let lowest = 'a';

for (let timestamp of timestamps) {
    if (lowest > timestamp) {
        lowest = timestamp;
    }
}

console.log('Lowest timestamp: ' + lowest)
// Lowest timestamp: 2023-03-15T10:46:20.395Z


Thanks to @Derpirscher for commenting this.


Useful reads:

Upvotes: 0

41 72 6c
41 72 6c

Reputation: 1780

Think you can solve this problem by adapting the loop in your case. The loop should look like this:

for (var i = 0; i < timestamps.length; i++) {
    var timeAsDate = Date.parse(timestamps[i]); # just adapted this to the loop
    ...
}

This should do the work for your case since the the loop before iterates over the keys of the array rather than the elements themselves which leads to a incorrect Date parsing because it tries to parse the index instead of the timestamp in that case.

Upvotes: 0

boppy
boppy

Reputation: 1908

If all dates are following the format mentioned, it's enough to string-sort that array and get the first entry, since the string is "sorted" from longest time-unit to shortest.

So to get the oldest entry:

const timestamps =  [
  '2023-03-15T10:47:38.878Z',
  '2023-03-15T10:46:51.375Z',
  '2023-03-15T10:46:39.645Z',
  '2023-03-15T10:47:19.072Z',
  '2023-03-15T10:46:20.395Z'
];
const lowest = timestamps.sort()[0];

Upvotes: 0

Related Questions