# How to JUST store hours and minutes in db?

**URL:** <https://www.ruby-forum.com/t/how-to-just-store-hours-and-minutes-in-db/124225>\
**Category:** Rails\
**Created:** [December 26, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/how-to-just-store-hours-and-minutes-in-db/124225 "2007-12-26T19:02:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [December 26, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/how-to-just-store-hours-and-minutes-in-db/124225/1 "2007-12-26T19:02:55Z")

</div>

I have a table for “restaurants” and need to store their weekly hours of  
operation. I am having trouble finding the best way to do this. I dont  
want to use a datetime field because I don’t need the date. I also want  
to be able to run queries and find all restaurants that are open at a  
given time. Any ideas on how to just store the time? Thanks for your  
help.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [December 26, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/how-to-just-store-hours-and-minutes-in-db/124225/2 "2007-12-26T19:10:56Z")

</div>

Why can’t you just store the time as a string? I believe if you use  
datetime field you can format it for just the time.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [December 26, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/how-to-just-store-hours-and-minutes-in-db/124225/3 "2007-12-26T19:11:57Z")

</div>

On Dec 26, 12:02 pm, Ben J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a table for “restaurants” and need to store their weekly hours of  
> operation. I am having trouble finding the best way to do this. I dont  
> want to use a datetime field because I don’t need the date. I also want  
> to be able to run queries and find all restaurants that are open at a  
> given time. Any ideas on how to just store the time? Thanks for your  
> help.

I would use a decimal field so 8:00 AM (0800) would be 8.0 and 4:30 PM  
(1630) would be 16.5.

AFAIK, there is no plugin to do this, but it shouldn’t be a difficult  
coding process.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [December 27, 2007, 12:17am UTC](https://www.ruby-forum.com/t/how-to-just-store-hours-and-minutes-in-db/124225/4 "2007-12-27T00:17:57Z")

</div>

Um… what is wrong with using a TIME field?

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [December 27, 2007, 3:11am UTC](https://www.ruby-forum.com/t/how-to-just-store-hours-and-minutes-in-db/124225/5 "2007-12-27T03:11:58Z")

</div>

On Dec 26, 5:17 pm, “Ryan B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Um… what is wrong with using a TIME field?

Ryan is correct.

In your migration

create\_table “products” do |t|  
t.column “opening\_time”, :time  
t.column “closing\_time”, :time  
end

–or in Rails 2–

create\_table “products” do |t|  
t.time “opening\_time”, “closing\_time”  
end
