How to get max or recent date on 2nd Column

How to get max or recent date on 2nd Column

spiderkznspiderkzn Posts: 48Questions: 14Answers: 0

I'm looking for getting maximum or most recent date on datatable (column 2) as it have list of date. How do I do that?

Answers

  • allanallan Posts: 64,210Questions: 1Answers: 10,597 Site admin

    It is possible to use the order option of selector-modifier objects to get data, as if it had been ordered in the table. So assuming your date data has been detected as dates, you could do:

    table.cells('*', 1, {order: 1})
      .data()
      .toArray()
      .reverse()[0]; 
    
    • Line 1: Select the cells from all rows for column index 1 (i.e. the second column), ordered by that column's data.
    • Line 2: Get the data for those cells
    • Line 3: Convert to a plain Javascript array
    • Line 4: Reverse (so the largest is first) and get the first item.

    Allan

  • spiderkznspiderkzn Posts: 48Questions: 14Answers: 0

    thank you for this. so I assume below that I need return latest date such as:

    let test1 = $("#person-history")
    .cells("*", 1, { order: 1 })
    .data()
    .toArray()
    .reverse()[0];

    alert(test1);

    I've tried this and it return error.

  • allanallan Posts: 64,210Questions: 1Answers: 10,597 Site admin

    cells() is a DataTable API method, so if you don't already have an API instance, you could try:

    let test1 = $("#person-history")
      .DataTable()
      .cells("*", 1, { order: 1 })
      .data()
      .toArray()
      .reverse()[0];
    
    alert(test1);
    

    Allan

Sign In or Register to comment.