0

How can i convert these strings in to date format and sort accordingly....please

2010-11-08 18:58:50.0_getCreated_10180  
2010-11-09 17:49:42.0_getCreated_10180  
2010-11-09 17:49:42.0_getCreated_10180  
2010-11-24 19:44:51.0_getCreated_10180  
2010-11-09 13:54:46.0_getCreated_10180  
2010-11-23 20:06:29.0_getCreated_10180  
2010-11-23 20:06:04.0_getCreated_10180  
2010-11-15 17:51:37.0_getCreated_10180 

Thanks in advance, Joseph

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
Joseph
  • 209
  • 7
  • 11

2 Answers2

7

If you have this in a single string then do.

// first create an array by splitting the string at the newlines
var list = dateString.split('\n'); 
list = list
    .map( // for each element in the list (each date)
        function(val,idx){
            // use the first part(before the dot(.)), replace the - with spaces and convert to date
            return new Date(val.split('.')[0].replace(/-/g,' '));
    })
    .sort(); // at the end sort the results.

example at http://www.jsfiddle.net/gaby/rfGv8/


What we need to do for each date (line) is

2010-11-08 18:58:50.0_getCreated_10180 (remove the part after the .)
accomplished with val.split('.')[0]

then replace the - with a space to make it look like 2010 11 08 18:58:50 which is an acceptable date format for the Date constructor.
accomplished with val.split('.')[0].replace(/-/g,' ')

Then pass it as a parameter to the constructor of Date to create a Date object
accomplished with new Date(val.split('.')[0].replace(/-/g,' '))

after applying the above to all elements and getting a new array use the .sort() method to sort the array in Ascending order.

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0

Please the following code. getDateSort function return the sorted date

 <script language="javascript" type="text/javascript">

            function getDateSort() {


                dateArray = new Array('2010-11-08 18:58:50', '2010-11-09 17:49:42',
    '2010-11-09 17:49:42', '2010-11-15 17:51:37', '2010-11-23 20:06:04', '2010-11-09 13:54:46', '2010-11-23 20:06:29', '2010-11-24 19:44:51');
                dateArray.sort(dmyOrdA);
                alert('Ascending : ' + dateArray + '');
                return false;
            }


            var dateRE = /^(\d{2})[\/\- ](\d{2})[\/\- ](\d{4})/;
            function dmyOrdA(a, b) {
                a = a.replace(dateRE, "$3$2$1");
                b = b.replace(dateRE, "$3$2$1");
                if (a > b) return 1;
                if (a < b) return -1;
                return 0;
            }
            function dmyOrdD(a, b) {
                a = a.replace(dateRE, "$3$2$1");
                b = b.replace(dateRE, "$3$2$1");
                if (a > b) return -1;
                if (a < b) return 1;
                return 0;
            }
            function mdyOrdA(a, b) {
                a = a.replace(dateRE, "$3$1$2");
                b = b.replace(dateRE, "$3$1$2");
                if (a > b) return 1;
                if (a < b) return -1;
                return 0;
            }
            function mdyOrdD(a, b) {
                a = a.replace(dateRE, "$3$1$2");
                b = b.replace(dateRE, "$3$1$2");
                if (a > b) return -1;
                if (a < b) return 1;
                return 0;
            }

        </script>
Amit Soni
  • 3,216
  • 6
  • 31
  • 50