Javascript ng repeat:仅按日期的时间部分排序
假设您有一组数据,其中包含一个实际上是日期/时间戳的属性。可以有任意天数,但在本例中,日期的唯一重要部分是日期的时间部分 如何仅使用时间在ng repeat表达式中对集合进行排序?它需要以前做一些操作吗 我目前正在尝试在从数据库填充时间戳属性后立即使用矩.js替换它Javascript ng repeat:仅按日期的时间部分排序,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,假设您有一组数据,其中包含一个实际上是日期/时间戳的属性。可以有任意天数,但在本例中,日期的唯一重要部分是日期的时间部分 如何仅使用时间在ng repeat表达式中对集合进行排序?它需要以前做一些操作吗 我目前正在尝试在从数据库填充时间戳属性后立即使用矩.js替换它 angular.forEach($scope.timeSlots, function (timeSlot, index) { timeSlot.BeginTime = moment(timeSlot.BeginTime).f
angular.forEach($scope.timeSlots, function (timeSlot, index) {
timeSlot.BeginTime = moment(timeSlot.BeginTime).format("hh:mm A");
timeSlot.EndTime = moment(timeSlot.EndTime).format("hh:mm A");
});
在模板中使用此值时,排序时仍会考虑日期。因此,根据时间是在哪一天添加的,它们可能会出现故障
<tr ng-repeat="timeSlot in timeSlots | orderBy: 'BeginTime'">
角度模板:
<tr ng-repeat="timeSlot in timeSlots | orderBy: 'BeginTimeDisplay'">
为什么不自己计算时间戳:
var date = new Date;
date.setTime(timeSlot.BeginTime);
var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hours = date.getHours();
timeSlot.ts = hours*60*60+minutes*60+seconds;
然后
我认为您应该将时间转换为秒数,并保存在BegintMedisPlay和EndTimeDisplay中,除非您正在显示这些属性,在这种情况下,您应该将它们保存在不同的属性中。如果您这样做,orderBy angularJS子句应该很容易工作。经过一点转换,因为日期/时间来自.Net,并且您的示例中有一个小的拼写错误修正(分钟/分钟),这就像一个符咒。谢谢太好了。很抱歉,错别字了,修好了。
var date = new Date;
date.setTime(timeSlot.BeginTime);
var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hours = date.getHours();
timeSlot.ts = hours*60*60+minutes*60+seconds;