Compare time in javascript

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

I have a flight table with departure times in the corresponding lines, what I need, I will put the time filter fields in my form to hide flights before and after the selected time. In other words, the visible intervals between the selected time interval will be visible.

I have no problem getting time information from the table and my inputs, but I don't know how to compare them. I am using jquery.

+1
javascript time
Jul 23 '10 at 7:52
source share
2 answers

No need jquery on this. Just old javascript.

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

+6
Jul 23 '10 at 7:55
source share

The easiest and fastest way to process data on the client side (sorting, filtering, grouping) is jOrder.

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

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

First, create a jOrder table from an array with a departure time index.

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

Then you can easily select a 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 user interface objects to fit the images.

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

0
Jul 24 '10 at 16:18
source share



All Articles