Last active
December 20, 2015 22:09
-
-
Save WenLiangTseng/6203231 to your computer and use it in GitHub Desktop.
jQuery 日期排序功能
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//reference from http://stackoverflow.com/questions/10636779/jquery-date-sorting | |
//But this version not working. don't know why. | |
function parseDate(input) { | |
var parts = input.match(/(\d+)/g); | |
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]]) | |
return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]); // months are 0-based | |
} | |
var elems = $.makeArray($(".dateDiv")); | |
elems.sort(function(a, b) { | |
console.log( parseDate( $(a).text() ) ); | |
return parseDate( $(a).text() ) < parseDate( $(b).text() ); | |
}); | |
$("#D").html(elems); | |
//This is a revised and working version: | |
function parseDate(input) { | |
var parts = input.match(/(\d+)/g); | |
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]]) | |
return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]); // months are 0-based | |
} | |
var elems = jQuery.makeArray(jQuery(".dateDiv")); | |
elems.sort(function(a,b){ | |
return parseDate( jQuery(a).data('date') ) - parseDate( jQuery(b).data('date') ); | |
}); | |
$("#D").html(elems); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="D"> | |
<div class="dateDiv">2012-04-15 10:25:45</div> | |
<div class="dateDiv">2012-04-10 19:41:08</div> | |
<div class="dateDiv">2012-04-20 07:00:10</div> | |
<div class="dateDiv">2012-04-12 16:45:50</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment