【问题标题】:Javascript date sorting by convert the string in to date format [closed]通过将字符串转换为日期格式的Javascript日期排序[关闭]
【发布时间】:2011-05-20 21:21:39
【问题描述】:

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

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 

提前致谢, 约瑟夫

【问题讨论】:

  • 这些字符串是如何生成的?

标签: javascript arrays string datetime sorting


【解决方案1】:

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

// 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.

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() 方法按升序对数组进行排序。

【讨论】:

  • 帮了很多忙...谢谢
【解决方案2】:

请输入以下代码。 getDateSort 函数返回排序后的日期

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

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2018-10-02
    • 2014-08-10
    相关资源
    最近更新 更多