Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using javascript how to sort date and timestamp (format of date is like this(ex:09/may/2014 13:45:21) timestamp in 24 hours format kindly help me on this
Posted

1 solution

Hi,
If all the dates are part of an array then they can be easily sorted by the below code.

JavaScript
//Lets say , you have all your dates in an array
var dates = [
  '09/may/2014 13:45:21' ,'31/dec/2012 13:45:21' , '09/may/2014 1:46:21' , '09/may/2013 22:45:21' ,'12/jan/2014 03:00:21' ]
  
//Then a sort function can be passed to the array.sort()
var sortedDates = dates.sort(function (var1, var2) { 
   var a= new Date(var1), b = new Date(var2);
    if (a > b)
      return 1;
    if (a < b)
      return -1;
   
    return 0;
});
/// Now the 'sortedDates' contain the dates in ascending order.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900