简体   繁体   English

如何使用 javasript 按日期对表格进行排序?

[英]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.在第 4 列之一中,有日期。 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我的日期示例:2021 年 4 月 29 日,晚上 8:26:07

I tried using parse, I tried jquery, I tried simply using Arrays.我尝试使用解析,我尝试了 jquery,我尝试简单地使用 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:您将获得 output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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