Strange behaviour with dates...

Strange behaviour with dates...

TonteriasTonterias Posts: 6Questions: 2Answers: 0

Hi,

When I list dates and I try to order them, it does something weird.

If I ask for .... $('#example').DataTable( { "order": [[ 2, "desc" ]], .... The datatable orders by date ascending, but within the same day it orders them descending.

I could find the place to change it. Is there any fix that I can use? I'm using Spring to send the data to the jsp .... modelAndView.setViewName("app.listmessages");

Thanks

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Just off the top of my head, I bet your dates are being sorted as strings, particularly if what I see on the page is exactly what is being set from the server. You might want to send the dates as regular dates '2017/05/06 10:45:34" then use something like moment.js to format your dates for display using render option

  • TonteriasTonterias Posts: 6Questions: 2Answers: 0

    Thanks bindrid...

    The dates are comming from to my JSP as: ${message.messagedate} (which I'm not sure if it is text)

    and then formatted like this:

    <fmt:formatDate pattern="EEEE',' d ' de ' MMMM ' de 'y 'a las' H:mm" value="${message.messagedate}" /></strong>

    Thanks again.

  • allanallan Posts: 63,482Questions: 1Answers: 10,467 Site admin

    I'm not certain what the output of that formatting is, but it doesn't look like it would be ISO8601, which is what DataTables supports for its built in date / time sorting. For anything else you would need to use MomentJS as @bindrid says. This blog post I wrote a while back uses MomentJS to sort virtually any format - you just need to tell it what format you want to use.

    Allan

This discussion has been closed.