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

brosej
  • 21
  • 1
  • 6

4 Answers4

1

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

Collin Grady
  • 2,226
  • 1
  • 14
  • 14
  • "*Just leave them as Date objects …*" I think you mean leave them as strings, they'll sort perfectly without any conversion. – RobG Aug 03 '18 at 03:37
  • 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. – Collin Grady Aug 03 '18 at 21:56
  • So just convert the single string to a Date rather than converting them all. – RobG Aug 04 '18 at 06:59
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))
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • 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. – Akrion Aug 02 '18 at 22:41
  • 1
    oops...my bad. Change to `new Date(b) - new Date(a)` to reverse the sorting...then you want the first one – charlietfl Aug 02 '18 at 22:54
  • You don't need to convert to Date at all, `arr.sort()` works as–is. ;-) – RobG Aug 03 '18 at 04:00
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.

Akrion
  • 18,117
  • 1
  • 34
  • 54
  • That actually looks beautiful, thank you, I´m trying to get better at this. – brosej Aug 02 '18 at 22:59
  • 1
    Why convert to Date at all? Just sort them as strings, that's one of the key benefits of using ISO 8601 format. – RobG Aug 03 '18 at 03:38
  • @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. – Akrion Aug 03 '18 at 04:33
  • "*Should*" is problematic, if the date format changes the built–in parser may not work anyway: [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Aug 03 '18 at 07:09
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());
RobG
  • 142,382
  • 31
  • 172
  • 209
  • If the down voter would care to explain their vote I could perhaps improve the answer. – RobG Aug 04 '18 at 07:00