1

Possible Duplicate:
Javascript date sorting by convert the string in to date format

Hi,

2010-11-08 18:58:50
2010-11-09 17:49:42
2010-11-09 17:49:42
2010-11-24 19:44:51
2010-11-09 13:54:46
2010-11-23 20:06:29
2010-11-23 20:06:04
2010-11-15 17:51:37
.....................
..............
..........

like this i have n number of string formats .How can i convert these strings in to date format and sort accordingly....please

Thanks in advance, Joseph

Community
  • 1
  • 1
Joseph
  • 209
  • 7
  • 11
  • _like this i have n number of string formats_ : Where? How? – Nivas Dec 06 '10 at 11:02
  • Looks like almost exact duplicate of http://stackoverflow.com/questions/4365116/javascript-date-sorting-by-convert-the-string-in-to-date-format – darioo Dec 06 '10 at 11:03
  • That only looks like one format to me, and a simple lexical sort will work on it. – cdhowie Dec 06 '10 at 11:04

3 Answers3

4

Since you are using ISO date formats you can simply sort them lexically.

var dates = [
    '2010-11-08 18:58:50',
    '2010-11-09 17:49:42',
    // and so on...
];

dates.sort();

For more information on sorting: http://www.w3schools.com/jsref/jsref_sort.asp

Philipp Jardas
  • 3,222
  • 3
  • 29
  • 42
1

The dates are already in a form that is suitable for sorting. So all you need to do is call array.sort().

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

i bet this one can help

how-can-i-convert-a-date-value-in-utc-format-to-a-date-object-in-javascript

Community
  • 1
  • 1
ovm
  • 2,452
  • 3
  • 28
  • 51