Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order by time of day and regardless of date

I have a Schedule model with attributes Day and Time. Time is of datatype time.

I have data i.e.

Monday / 2014-04-26 15:00:00.000000

Monday / 2014-04-25 16:30:00.000000

In my controller I have @schedule = Schedule.all(:order => 'Day, Time')

Right now it is ordering by date, how do I order by time regardless of date?

like image 363
Bruno Avatar asked Sep 30 '22 22:09

Bruno


1 Answers

If you're using Postgres you can use

@schedule = Schedule.all.order('date_time_field::time').

If you're on Mysql I think it's something like

DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY

Sqlite

@schedule = Schedule.all.order('time(date_time_field)')
like image 119
Eyeslandic Avatar answered Oct 03 '22 13:10

Eyeslandic