views:

44

answers:

3

I need to create a function to filter data according to time.

I have a table of flights with departure time in related rows, what i need is, i will put to time filter fields to my form, for hiding flights before and after selected time. In other words, flighs bettween selected time interval will be visible.

I have no problem with getting time info from table and my inputs, but i do not now how to compare them. I use jquery.

Thanks for your help

+3  A: 

No need for jquery on this one. Just plain old javascript.

The easiest way is to just convert the date objects to unix time using getTime method (I think that is the name). Then just do a greater/less than comparison to see if the values are within the range.

spinon
Hurray for javascript date object!
Igor Zinov'yev
A: 

You should have a look at http://www.datejs.com/

jAndy
A: 

The easiest and fastest way of doing client side data manipulation (sorting, filtering, grouping) is jOrder.

In your case, I assume the data looks something like this: (date1 and date2 being date objects)

var data = [{flight: '776', departure: date1}, {flight: '51', departure: date2}];

First thing, create a jOrder table out of the array, with index on departure time.

var table = jOrder(data)
    .index('departure', ['departure'], {grouped: true, ordered: true});

Then, you can easily select the row with dates in the specified range.

 var hits = table.where([{ departure: {lower: datelow, upper: datehi}}], {mode: jOrder.range});

Finally you can rebuild or modify the UI objects according to the hits.

jOrder is available at http://github.com/danstocker/jorder.

Dan Stocker