JavaScript按表上的日期排序不起作用

嗨我无法在日期上获得排序function。 它会改变行的顺序,但它没有正确排序,日期遍布整个地方。

我希望将来的活动能够应对未来的活动。

请参阅下面的代码。

$('tr.Entries').sort(function(a,b){ return new Date(jQuery(a).find('td.event-date > a').text()).getTime()  a').text()).getTime() }).appendTo('tbody'); 
  
Date Event Location Ticket
25/08/2017 Live N Local Winter Music Festival Pause Bar, Balaclava
15/04/2017 Reggae, Seggae & Sega Night Bar Open, Fitzroy Melbourne
06/05/2016 The Sunraysia Multicultural Festival Nowingi Place, Mildura Victoria

谢谢!

UPDATE

我似乎在一个数组中添加日期然后对它们进行排序 – 现在我只是将它们追加到身体上? 谢谢

 var dates = jQuery('tr.Entries').find('td.event-date > a').map(function() { return jQuery(this).text(); }).get(); dates.sort(function(a,b){ var dateA = a; dateA = dateA.replace(/(..)\/(..)\/(....)/, '$2-$1-$3'); var dateB = b; dateB = dateB.replace(/(..)\/(..)\/(....)/, '$2-$1-$3'); return new Date(dateA).getTime() - new Date(dateB).getTime(); }); jQuery.each(dates, function (index, value) { //how to append back to tbody? console.log(value); }); 

您的日期字符串格式不正确。 您可以使用string#replace()将日期字符串从DD\MM\YYYY更改为MM-DD-YYYY DD\MM\YYYY ,然后转换为日期并进行比较。

 $('tr.Entries').sort(function(a,b){ var dateA = jQuery(a).find('td.event-date > a').text(); dateA = dateA.replace(/(..)\/(..)\/(....)/, '$2-$1-$3'); var dateB = jQuery(b).find('td.event-date > a').text(); dateB = dateB.replace(/(..)\/(..)\/(....)/, '$2-$1-$3'); return new Date(dateB).getTime() - new Date(dateA).getTime();; }).appendTo('tbody'); 
  
Date Event Location Ticket
15/04/2017 Reggae, Seggae & Sega Night Bar Open, Fitzroy Melbourne
06/05/2016 The Sunraysia Multicultural Festival Nowingi Place, Mildura Victoria
25/08/2017 Live N Local Winter Music Festival Pause Bar, Balaclava

new Date()不知道如何解析第一部分中具有该日期的日期而不是该部分。 Date需要格式MM/DD/YYYY

如果您在25/08/201725/08/2017日第一次约会并将其传递到new Date()您将收到Invalid Date回复。

如果你希望这个工作,你需要确保日期格式化为Date理解的东西,这样你可以以编程方式进行交换,或者你也可以手动将参数传递给new Date函数