3

I have a SP2010 list which contains a column with dates. (Date created).

I pull these using Spservices which comes in the following format:YYYY.MM.DD 00:00.

In the main function which reads the list I split the date so all that remains is YYYY.MM.DD.

dateAdded = ($(this).attr("ows_DatumActualiteit")).split(" ")[0];

The dates are then pushed in to an array for later filtering.

function selectMostRecentAct(datesIn){
    var dateArray = [];
    dateArray.push(datesIn);
    
    for (var i = 0; i < dateArray.length; i++) {
    console.log(dateArray[i]); //spits out all the dates in the array.
        
};

I want to select the most recent date in this array. How can I achieve this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

4 Answers4

1

This question seems similar to yours: What is the elegant way to get the latest date from array of objects in client side?

An alternative would be to loop through the whole array and compare the dates. As far as I know, you can use the <, >, == operators with dates.

dateArray.forEach((e) => {
    if (e > latestDate)
        latestDate = e;
});
console.log('Latest date is: ' + latestDate);
zlikar
  • 84
  • 2
  • 1
    Most people forget that timestrings can be easily sorted without complicated Date conversion. +1 for providing the easiest and probably fastest answer, however you should definetly declare `latestDate` ... – Jonas Wilms Mar 26 '18 at 20:35
  • Thanks and yes exactly, you should define the latestDate variable outside the loop (and set it to the earliest possible date?) – zlikar Mar 26 '18 at 20:54
  • I think Jonas was suggesting that you should edit the answer to include that initialization. – Scott Sauyet Mar 26 '18 at 21:21
1

You need to sort the values then take the first element.

I'm assuming the format of your dates, but new Date is very lenient and should accept most reasonable inputs.

const dates = [
  "2018-03-01T10:30:12.000Z",
  "2018-03-01T12:11:49.000Z",
  "2018-03-12T15:54:49.000Z",
  "2018-03-09T19:12:49.000Z",
  "2018-03-03T01:41:49.000Z",
];

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

console.log(selectMostRecent(dates));

Sort wants a comparison function that returns a value, either positive, negative or zero. When you perform arithmetic on a date value it is converted to epoch time (e.g. 1522096404277, milliseconds since 01/01/1970), and then subtracting these gives us the signed value we desire.

For example,
2018-03-09T19:12:49.000Z returns 1520622769000
2018-03-03T01:41:49.000Z returns 1520041309000

And when we do 2018-03-09T19:12:49.000Z - 2018-03-03T01:41:49.000Z (but those are coerced to numbers as described above) we get 581460000, meaning that the first date is sorted above the latter one.

Jack
  • 804
  • 7
  • 18
0

Maybe something like this:

dateArray
   .map(d => new Date(d.replace(/\./g, "-"))) // ISO format
   .sort((a, b) => b - a) // Newest to oldest, probably
   [0] // First!
H.B.
  • 166,899
  • 29
  • 327
  • 400
0

This uses a reusable function for max; it should work for any values that you can compare with >. Note that it requires you to set a minimum value. For strings, "" (the empty string) seems appropriate. For numbers, you could use -Infinity, although that case is already covered by Math.max. For actual dates (not these dates formatted as strings), you can choose any date that would be well before any conceivable values, perhaps something like new Date(-1e15).

const max = (min) => vals => vals.reduce((curr, val) => val > curr ? val : curr, min)
const maxDate = max('')

console.log(
  maxDate(['2014.07.23', '2014.08.29', '2007.03.25', '2017.09.30', '2008.02.29'])
)
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103