gpt4 book ai didi

javascript - Angular JS - 按ng-repeat表上的entry_date排序仅按月和日而不是年排序

转载 作者:行者123 更新时间:2023-11-28 04:59:59 26 4
gpt4 key购买 nike

我有一个 Angular ng-repeat 语句,我需要按日期字段 (entry_date) 进行排序。到目前为止,仅使用以下代码按月和日排序:

<tbody ng-repeat="transaction in transactions | orderBy:'-entry_date'">
<tr>
<td>
<input ng-model="transaction.selected" type="checkbox"/>
</td>
<td>
<span>{{ transaction.entry_date | date:'yyyy-MM-dd HH:mm' }}</span>
</td>
<td>
<span>{{ transaction.description }}</span>
</td>
</tr>
</tbody>
</table>

这是从控制台提取的输入日期值:“2011-06-30T08:17:35Z”

这是第一页的日期表:

2011-06-30 09:17    
2011-06-29 12:47
2011-06-29 08:20

这是最后一页:

2017-02-12 01:00    
2017-02-12 01:00
2017-02-12 01:00
2017-02-12 01:00
2017-02-11 01:00

我也尝试过使用回调函数:

  $scope.sortDates = function(transaction) {
var date = new Date(transaction.entry_date);
return date;
};

但这只是以与我的原始代码相同的方式返回数据。

有没有办法让过滤器使用年份和月份/日期?

提前致谢。

最佳答案

实际上,我认为你需要返回一个整数,如果整数是正数,则第一个元素比第二个元素大。
如果它是负数,则它比第二个小,如果整数等于零,它们都是相同的。
您可以使用 moment 来获取两个日期的 unix 时间戳并减去它们。
/> 类似于

function(a, b){
return moment(a).unix() - moment(b).unix();
}

我还没有测试过代码。

关于javascript - Angular JS - 按ng-repeat表上的entry_date排序仅按月和日而不是年排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42205212/

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