mercredi 3 février 2016

Obtaining datetime value from database and getting its hour int

I am new to Ruby/Ruby on Rails and I was wondering if it is possible to get a datetime entry :starts_at from my events table in my database and obtain from it an integer representing just the hour value?

The datetime ':starts_at'has the layout:

YYYY-MM-DD HH:MM:SS
e.g. 2016-01-03 11:05:00

what I would want is just the "11" representing the hour, so I can set it as variable "start_hour".

I am not sure how to obtain my :starts_at value from the database in the first place, but here is some attempt at code showing what I mean.

Doing:

events.each do |event|
    start_time = event(:starts_at.hour) #is this how to obtain :starts_at?
end

would return "11:00" instead of "11", how would I narrow it down to just "11"? And if the time was "08:00" it should be "8"?

Aucun commentaire:

Enregistrer un commentaire