gpt4 book ai didi

javascript - 带有日期字符串的 Angular orderBy

转载 作者:行者123 更新时间:2023-12-02 16:39:10 25 4
gpt4 key购买 nike

我正在尝试按日期对网络应用程序中的事件进行排序(在本例中为数据库中的“开始”日期)。我遇到了日期排序不正确的问题。我相信他们是按字母数字排序而不是按日期排序,因为我的“开始”字段是一个字符串。我的问题是,如何最好地将此字段转换为日期以便正确排序?相关代码如下:

html:

<div ng-repeat="event in events | orderBy:'start'">{{event.city}}</div>

Angular Controller :

angular.module('MainCtrl', []).controller('MainController', function($scope, $routeParams, Events) {

// pull empty event details from API based on routeParams to get all events
$scope.id = '';

Events.get($scope.id).success(function(response) {
$scope.events = response;
});
});

示例数据库对象:

{
"_id": {
"$oid": "54978d87ac1e3caaeee8452c"
},
"event": "New Years Eve - Times Square",
"city": "New York",
"state": "New York",
"date": "December 31, 2014",
"start": "December 31, 2014",
"end": "January 1, 2015",
"radius": "400",
"team_1": "",
"team_2": "",
"object_id": "10897094",
"longitude": "-73.9848931",
"latitude": "40.7591529",
"photos": []
}

最佳答案

结果证明这是一个非常简单的解决方案。我只是将以下代码添加到 Events.get 内的 Controller 中:

for (var i = 0, max = response.length; i < max; i++) {
$scope.events[i].start = new Date(response[i].start);
}

关于javascript - 带有日期字符串的 Angular orderBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com