3

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
);
evanx
  • 1,291
  • 4
  • 17
  • 33
  • So you're just looking to represent a value like "9:30 am"? – Larsenal Jan 17 '13 at 20:20
  • yes, and I want to be able to compare the times. – evanx Jan 18 '13 at 01:28
  • If you use `time` format for the column type, MySQL (for example) will store it as just the time, which is great, but Rails will then convert that to a proper Time (with Time Zone) and attach a Date to it and a Time Zone, which is not ideal in most cases. – Joshua Pinter May 30 '19 at 16:29

1 Answers1

8

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.

Community
  • 1
  • 1
ply
  • 1,141
  • 1
  • 10
  • 17