javascript日期排序由转换字符串中的日期格式 [英] Javascript date sorting by convert the string in to date format

查看:251
本文介绍了javascript日期排序由转换字符串中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何转换这些字符串中的日期格式和排序相应....请

  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

在此先感谢,
约瑟夫


解决方案

如果你在一个字符串有这个然后做。

  //首先通过在新行分割字符串创建一个数组
VAR列表= dateString.split('\\ n');
清单列表=
    .MAP(//对于列表中的每个元件(每个日期)
        功能(VAL,IDX){
            //使用第一部分(点()之前),更换 - 用空格并转换为日期
            返回新的日期(val.split()[0] .replace(/ - /克,'')'。');
    })
    。分类(); //在结束对结果进行排序。

http://www.jsfiddle.net/gaby/rfGv8/ <例子/ p>


我们所需要的每个日期做(的的)是

2010-11-08 18点58分50秒<击> .0_getCreated_10180 后取出一部分。的)结果
与完成 val.split('。')[0]

然后再更换 - 一个空间,使它看起来像 2010 11 08 18时58分50秒这是一个可以接受的日期格式日期的构造。结果
与完成 val.split('。')[0] .replace(/ - /克,'')

然后它作为参数传递到日期的构造函数创建一个Date对象结果

- 用新的日期('。'/克,'')val.split()[0] .replace(/)完成

运用以上所有元素并得到一个新的数组使用的.sort()方法在升序排序的数组后。

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

解决方案

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.

这篇关于javascript日期排序由转换字符串中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆