0

I have an array of dates like this

arr = ["2018-08-01T15:16:34.791Z","2018-08-01T15:16:34.791Z","2018-08- 
01T15:16:34.791Z"]

So I tried converting those dates to time:

convertedArr = arr.map( payment => {
    var converted
    converted = new Date(payment.date_updated).getTime()
    return converted
  })

-And then sort them out and get the highest amount of time through another function:

getLatestPayment(convertedArr){

const sortFromHighest = arr => {
  return arr.sort( (a,b) => { return a < b } )
}
var convertedArr = convertedArr
convertedArr = sortFromHighest(convertedArr)
convertedArr = convertedArr.map( elem =>{
  new Date(elem)
})

return paymentsArr[0]

}

Then I get an undefined value after that because I can't do this convert the dates from time back to dates again, is there an actual way to make to get the dates from time? or perhaps a different solution? thanks in advance

2
  • 1
    Possible duplicate of Min/Max of dates in an array? Commented Aug 2, 2018 at 22:11
  • Just sort them as strings. Commented Aug 3, 2018 at 3:58

4 Answers 4

1

Just leave them as Date objects before sorting; don't call getTime() on them, because you can already compare Dates with < and >

Sign up to request clarification or add additional context in comments.

3 Comments

"Just leave them as Date objects …" I think you mean leave them as strings, they'll sort perfectly without any conversion.
No, I meant to leave them as Date objects because his code as presented is trying to return a single Date object at the end, not a string.
So just convert the single string to a Date rather than converting them all.
0

You could also go with somewhat more functional chained approach:

var arr = ["2011-08-01T19:16:34.791Z","2018-08-01T11:11:34.791Z","2012-08-01T15:16:34.791Z"]

console.log(arr.map((x) => new Date(x)).sort().slice(-1))

However it would be less performant for very large arrays.

4 Comments

That actually looks beautiful, thank you, I´m trying to get better at this.
Why convert to Date at all? Just sort them as strings, that's one of the key benefits of using ISO 8601 format.
@RobG that was the first thing I tried and wanted to post as a solution, but it is not as generic. If the date format changes as long it is a valid format this should work.
"Should" is problematic, if the date format changes the built–in parser may not work anyway: Why does Date.parse give incorrect results?
0

If you want the original string value can create the date objects in sorter and skip the mapping

const arr = ["2018-08-01T15:16:34.791Z","2018-08-01T15:16:34.791Z","2018-08-01T15:16:34.791Z"];

const getLatest = (arr) => arr.sort((a,b) => new Date(b) - new Date(a))[0];

console.log(getLatest(arr))

3 Comments

Why [0] index with date subtraction? Should be > since with input: var arr = ["2018-08-01T19:16:34.791Z","2011-08-01T11:11:34.791Z","2018-08-01T15:16:34.791Z"] your code returns the smallest date.
oops...my bad. Change to new Date(b) - new Date(a) to reverse the sorting...then you want the first one
You don't need to convert to Date at all, arr.sort() works as–is. ;-)
0

Given you've got UTC ISO 8601 strings, they will sort as strings. So just sort them and grab the first or last element, depending on whether you wan the oldest or newest date.

If you want the result as a Date, just convert the last one (but take note of Why does Date.parse give incorrect results?

var arr = [
    "2018-08-01T15:16:35.791Z",
    "2018-08-01T15:16:34.791Z",
    "2018-08-05T15:16:34.791Z",
    "2018-08-02T15:16:34.791Z"
    ];
    
console.log('Oldest: ' + arr.sort().slice(0,1));
console.log('Newest: ' + arr.sort().slice(-1));
console.log('Newest: ' + new Date(arr.sort().slice(-1)).toString());

1 Comment

If the down voter would care to explain their vote I could perhaps improve the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.