Rails. How to store time of day (for schedule)?

Viewed 20590

I'm writing an app that keeps track of school classes.

I need to store the schedule. For example: Monday-Friday from 8:am-11am.

I was thinking about using a simple string column but I'm going to need to make time calculations later.

For example, I need to store a representation of 8am, such as start_at:8am end_at:11am

So how should I store the time? What datatype should I use? Should I store start time and number of seconds or minutes and then calculate from there? or is there an easier way?

I use MySQL for production and SQLite for development.

8 Answers

If you're using Postgresql you can use a time column type which is just the time of day and no date. You can then query

Event.where("start_time > '10:00:00' and end_time < '12:00:00'")

Maybe MySQL has something similar

Related