0

I'm trying to write my own query/filter function for my AngularJs app.

I have a JSON with a lot of posts like this

[
{
    "date": "20100101",
    "keyword": "blah",
    "header": "My header",
    "text": "aaa, bb, c, dd. ee f g h."
},
{
    "date": "20110101",
    "keyword": "blah-hah",
    "header": "Your header",
    "text": "foo and bar and then some"
}

]

In my app I have a small form where the user enters "date from", "date to", and then optionally a keyword.

   <input type="text" class="form-control" ng-model="dateFrom">
   <input type="text" class="form-control" ng-model="dateTo">
   <input type="text" class="form-control" ng-model="keyword">

The user add his/her criteria and push a button, my fabolous function loops through the JSON and all posts with a date between the criterias (if entered), and a matching keyword (if entered), will have their "text" property added to a string, bound to textarea for show.

To me, it looks like a simple loop where I check the given criterias, but how do I handle the date comparsion in a sane way? using Date.Parse()? Or are there any Angular helpers?

Regards

elwis
  • 1,395
  • 2
  • 20
  • 34
  • There are lots of other questions about comparing dates for you to find an answer on, but the general jist is to convert both dates to a javascript date object and compare the two using `.getTime()` which ensures you are comparing two millisecond representations of the date. – Mark Walters Nov 20 '13 at 12:39
  • Ok, thanks. So there are no shortcuts, not to smooth so I thought Angular perhaps had a cleaner solution. But if that's the way to do it, then I will, thanks :) – elwis Nov 20 '13 at 12:45
  • Saying that your using `"date" : "20110101"` in your JSON so you could manipulate the user entry to match that format of date and test on string comparison `userDate === yourJSON[i]["date"]`. – Mark Walters Nov 20 '13 at 12:47
  • @Mark, yes I thought about that but I'm not sure if that will work good enough comparing >= dateFrom and <= dateTo.. I'm not that comfortable with javascript yet. – elwis Nov 20 '13 at 12:51

0 Answers0