开发者

Compare time in javascript

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜