javascript OrderBy 日期值,它只是 Angular JS 中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25306216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:20:58  来源:igfitidea点击:

OrderBy Date values, which are just strings in Angular JS

javascriptangularjsdateangularjs-orderby

提问by

I'm trying to order some data by date, although the dates are just strings, in the format dd-mm-yyyy.

我正在尝试按日期排序一些数据,尽管日期只是字符串,格式为 dd-mm-yyyy。

I made a filter which converted the plain string of numbers (which where in the US date format, where I wanted the UK date format) e.g 01272012 to 27-01-2014, but when I try to order them it still only sees them as numeric strings, so 01-01-1990 would come before 02-01-2014.

我制作了一个过滤器,可以将纯数字字符串(在美国日期格式中,我想要英国日期格式的地方)例如 01272012 转换为 27-01-2014,但是当我尝试订购它们时,它仍然只将它们视为数字字符串,所以 01-01-1990 会在 02-01-2014 之前出现。

Any suggestions of how to do this in a filter?

有关如何在过滤器中执行此操作的任何建议?

Thanks!

谢谢!

Update

更新

I figured out the dates would automatically get orderedif the date format was yyyy-mm-dd. I then used used orderBy:['date']to order the data, only using my original filter when displaying the data.

我发现如果日期格式是 yyyy-mm-dd,日期会自动排序。然后我习惯于orderBy:['date']对数据进行排序,仅在显示数据时使用我原来的过滤器。

I ended up having to reverse my data, showing the most recent dates. To achieve this I added a -to my orderBy statment: orderBy:['-date'].

我最终不得不反转我的数据,显示最近的日期。要做到这一点,我添加一个-到我的排序依据statment: orderBy:['-date']

回答by David Bohunek

Because orderByfilter, you can use that. Your ng-repear would probably look similar to this:

因为orderBy过滤器,你可以使用它。您的 ng-repear 可能类似于:

ng-repeat="item in items | orderBy: orderByDate"

and then on your controller you would define the orderByDatefunction:

然后在您的控制器上定义orderByDate函数:

$scope.orderByDate = function(item) {
    var parts = item.dateString.split('-');
    var date = new Date(parseInt(parts[2], 
                        parseInt(parts[1]), 
                        parseInt(parts[0]));

    return date;
};

Implementation of the function is up to you. I choose to create Datefrom the string. The object you return from orderByDatefunction is then used to be order with using <, =, > operators.

该功能的实现取决于您。我选择Date从字符串创建。然后,您从orderByDate函数返回的对象将使用 <、=、> 运算符进行排序。

EDIT:solution for :reverse

编辑:解决方案:reverse

Since :reversecan't be used with a function passed as parameter, you can implement your custom function to return reversed value itself. In such case using Dateis not possible, I would construct a number then and returned it with minus:

由于:reverse不能与作为参数传递的函数一起使用,您可以实现自定义函数以返回反向值本身。在这种情况下使用Date是不可能的,我会构造一个数字然后用减号返回它:

$scope.orderByDate = function(item) {
    var parts = item.dateString.split('-');
    var number = parseInt(parts[2] + parts[1] + parts[0]);

    return -number;
};

回答by Archana

var now = Date.now(); now = $filter('date')(now, 'yyyy-MM-ddThh:mm:ss+hh:mm');

var now = Date.now(); now = $filter('date')(now, 'yyyy-MM-ddThh:mm:ss+hh:mm');

Then you can data filtering with corresponding formatted date

然后您可以使用相应的格式化日期进行数据过滤