0

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

Getting data from api want sort by _title enter image description here

3
  • add your html and javascript Commented Jan 5, 2017 at 13:51
  • Where a you while converting the date ? in a controller ? in the view ? You can usually sort dates in a ng-repeat with an orderby filter Commented Jan 5, 2017 at 13:52
  • with orderBy : _title it sorting by first digit means 05.11.12 and 06.12.11 first will be with 05 Commented Jan 5, 2017 at 13:55

1 Answer 1

1

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>

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.