开发者

Javascript date sorting by convert the string in to date format [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.

Closed 8 years ago.

Improve this question

How can i convert these strings in to开发者_StackOverflow社区 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


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜