简体   繁体   中英

How to sort table by date using javasript?

I have a responsive table that displays the data it's given. In one of the 4th columns, there're the dates. I simply want a button to sort the table by the date in that column.

An example of the dates I have: 4/29/2021, 8:26:07 PM

I tried using parse, I tried jquery, I tried simply using Arrays. Yet I have failed to properly arrange the table by date.

I would be grateful if someone could tell me how to tackle this problem.

Example:

 var array = ["4/28/2021, 8:26:07 PM","4/29/2021, 8:26:07 PM"];
 array.sort((a, b) => new Date(b).getTime() - new Date(a).getTime());

You will get the output:

["4/29/2021, 8:26:07 PM","4/29/2021, 8:26:07 PM"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM