如何排序字符串日期angularjs [英] How to sort string date angularjs

查看:74
本文介绍了如何排序字符串日期angularjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对字符串日期格式进行排序24.01.2017(降序)
我尝试使用 Date.parse('24 .01.2017'); ->但格式不正确

How sort string date format 24.01.2017 ( descending ) I tried with Date.parse('24.01.2017'); -> but incorrect format

日期如何排序?
在控制器中或查看
谢谢

How sort date like this? in controller or view Thanks

从api
获取数据要按 _title

Getting data from api want sort by _title

推荐答案

尝试使用 orderBy 具有自定义比较功能的过滤器:

Try the orderBy filter with a custom comparing function:

JS:

$scope.entries = [
  {date: '05.02.2001'},
  {date: '01.20.1930'},
  {date: '03.20.2020'} 
]

$scope.compareDates = function(date1, date2) {
  console.log(date1)
  var split1 = date1.value.split('.');
  var split2 = date2.value.split('.');

  var date1compare = split1[2] + split1[1] + split1[0];
  var date2compare = split2[2] + split2[1] + split2[0];

  return (date1compare > date2compare ? 1 : -1)
}

HTML:

<div ng-repeat="entry in entries | orderBy:'date':false:compareDates">
    {{entry.date}}
</div>

柱塞: https://plnkr.co/edit/LETAFDoD5fub63tKI5Ne?p=preview

这篇关于如何排序字符串日期angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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