通过将字符串转换为日期格式来进行Javascript日期排序

Jos*_*eph 1 javascript arrays sorting string datetime

如何将这些字符串转换为日期格式并相应地排序....请

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 
Run Code Online (Sandbox Code Playgroud)

先谢谢你,约瑟夫

Gab*_*oli 7

如果你在一个字符串中有这个,那么.

// 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.
Run Code Online (Sandbox Code Playgroud)

http://www.jsfiddle.net/gaby/rfGv8/上的示例


我们需要为每个日期()做什么

2010-11-08 18:58:50 .0_getCreated_10180(删除后的部分.)
完成了val.split('.')[0]

然后用空格替换 - 使其看起来像构造函数2010 11 08 18:58:50的可接受日期格式Date.
完成了val.split('.')[0].replace(/-/g,' ')

然后将它作为参数传递给Date的构造函数,以创建一个
完成的Date对象new Date(val.split('.')[0].replace(/-/g,' '))

将上述内容应用于所有元素并获取新数组后,使用该.sort()方法按升序排序数组.