Shabini Rajadas Shabini Rajadas - 1 year ago 64
Ruby Question

Select records created between a time range irregardless of the date of creation

I want to select a set of records created between 2 am to 3 am, irregardless of the date it was created. Is there any way to achieve this?

Answer Source

With PostgreSQL:

Model.where("created_at::time BETWEEN '14:00:00' AND '15:00:00'")

Is there any possibility to check between 23:00:00 to 01:00:00.


Model.where("created_at::time >= '23:00:00' AND created_at::time < '01:00:00'")

P.S. Before dealing with time queries make sure you are aware of the Rails default timezone (UTC) and issues connected to users' timezone differences.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download