evanx
evanx

Reputation: 1301

How to store only the time in the database?

I'm using rails 3, I want to store start_time and end_time. Since rails does not support date ranges.

which data type should I use to store the time without the date to later make comparisons?

This is my database schema with the tsrange data types in the table Schedules (which datatype should I use to replace it?).

CREATE TABLE Doctors
(
id INTEGER UNIQUE,
name VARCHAR(size)
);

CREATE TABLE Schedules
(
id INTEGER,
doctor_id INTEGER,
start_time tsrange,
end_time tsrange,
day VARCHAR(size)
);

CREATE TABLE Appointments
(
id INTEGER,
doctor_id INTEGER,
aday DATE
);

Upvotes: 3

Views: 4706

Answers (1)

ply
ply

Reputation: 1141

The :time datatype is probably what you're looking for.

Also, this question really helped me when I was trying to learn about the differences between date/time datatypes in rails:

In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date?

You should also consider using datetime datatype , and simply converting it using strftime, e.g

 t = Time.now
 t.strftime("%I:%M%p")

This will take a date/time and simply format at as Hour/Minute/AM|PM, leaving out the date.

Upvotes: 8

Related Questions