如何在JavaScript中对日期字符串进行排序(格式示例:2014 7 23)? [英] How to sort date strings (format example: 2014 7 23) in JavaScript?

查看:1557
本文介绍了如何在JavaScript中对日期字符串进行排序(格式示例:2014 7 23)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我试图按日期对一组对象进行排序,每个对象都有一个日期,但是遇到了一个障碍。另外,日期是从网站上的3个下拉框中输入的,所以我只需要3个数字。这意味着我不能使用JavaScript的Date()对象,因为它还会添加时间,时区并以字母等形式写月份名称。

In JavaScript I'm trying to sort an array of objects, each having a date, by date, but I ran into an obstacle. Also the date's are input from 3 dropdown boxes on a site, so I just want 3 numbers. This means I cannot use JavaScript's Date() object, since it also adds a time, timezone and writes month names in letters etc.

示例:
将5个对象放入一个数组。我尝试使用JavaScript排序功能,具体来说就是这样:

Example: I added 5 objects into an array. I have tried using the JavaScript sort function, this one to be specific:

array.sort(function(a, b) {
    if(a.date == b.date){
        return 0;
    }
    else if (a.date < b.date){
        return 1;
    } 
     else {
        return -1;
    }
})

但是,这仅按年份排序。

However, this only sorts by year.

因此,如果我加上:
2014 7 12
2017 8 16
2017 4 14
2017 1 31
2017 2 26

So If I add: 2014 7 12 2017 8 16 2017 4 14 2017 1 31 2017 2 26

我得到:
2014 7 12
2017 2 26
2017 8 16
2017 1 31
2017 4 14

I get: 2014 7 12 2017 2 26 2017 8 16 2017 1 31 2017 4 14

这是我用来构造的构造函数

This is the constructor I use to make different Label objects.

function Label(name, date, type) {
this.name = name;
this.date = date;
this.type = type;

}

var a = new Label("name1", "2016 5 16", 5);
var b = new Label("name2", "2016 7 20", 3);
var c = new Label("name3", "2016 3 15", 2);

我的日期属性只是一个字符串中的3个数字。因此,我尝试将排序函数重写为:

My date attributes are just 3 numbers in a string. So I tried rewriting the sort function to this:

    array.sort(function(a,b){
        a = a.date.split(" ");
        b = b.date.split(" ");
        if(a[0] === b[0] && a[1] === b[1] && a[2] === a[2]){
            return 0;
        }
        else if ((a[0] > b[0]) || (a[0] === b[0] && a[1] > b[1]) || (a[0] === b[0] && a[1] === b[1] && a[2] > b[2])){
            return -1;
        } 
        else {
            return 1;
        }
    });

即我尝试使用.split函数分隔3个数字,然后将其存储在数组中。然后为日期a和3分配数组3个数字。然后,我检查两个数组中的值是否相等,如果相等,则返回0(不执行任何操作)。
如果数组a中的年份大于数组b中的年份,请将其在输出数组中下移1个空格。
如果年份相等,则检查月份。如果a中的月份大于b中的月份,请在输出数组中向下移动1个空格。
等。

I.e. I tried to use the .split function to seperate the 3 numbers, which are then stored in an array. Date a is then assigned the array with 3 numbers, as is date b. Then I check if the values in both arrays are equal, if so, return 0 (do nothing). If the year in array a is bigger than year in array b, move it down 1 space in the output array. If the year is equal, then check the month. If the month is bigger in a than in b, move a down 1 space in the output array. Etc.

但是这种情况不会发生。它基本上给了我相同的输出,只按年份排序,而忽略了月份和日期。

However this doesn't happen. It basically gives me the same output, only sorting by year but ignoring month and day.

我检查了几个类似的StackOverflow问题,但大多数问题都使用Date()目的。
我特别需要格式数字数字。

I have checked several similar StackOverflow questions, but most of them use the Date() object. I specifically need the format "number number number".

为什么我的函数不起作用,怎么使它起作用?

Why doesn't my function work and how might I make it work?

编辑:使用日期和构造函数的示例使我的帖子更加清晰。

made my post a bit clearer with examples of dates and my constructor.

推荐答案

看起来日期格式总是与您已经分割的日期相同,因此您应该使用日期对象并进行比较

Looks like the date format is always the same as you're already splitting etc. and what you should do is use date objects and compare them instead

array.sort(function(a,b){
    var arr1 = a.date.split(" ");
    var arr2 = b.date.split(" ");

    var time1 = new Date(arr1[0], arr1[1]-1, arr1[2]); // year, month, day
    var time2 = new Date(arr2[0], arr2[1]-1, arr2[2]);

    return time1 - time2;
});

这篇关于如何在JavaScript中对日期字符串进行排序(格式示例:2014 7 23)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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