John Smith
John Smith

Reputation: 6259

Minutes between two Times

First thing i have problems with is that, although i defined in my database:

 add_column :patients, :wartezeit, :time

And in my controller:

 @patient.update_columns(wartezimmer: true, wartezeit: Time.now)

I get such an ouput for any patient.wartezeit:

2000-01-01 13:48:18 UTC i mean this is Datetime! Im right? What did i wrong?

Next i tried to get the minutes between the patient.wartezeit and Time.now:

((Time.now - f.wartezeit) / 60).to_i + " minutes"

Somehow i get a complete wrong number of minutes! Probably because patient.wartezeit is defined as Datetime.

Maybe someone also has a idea for me how i have to change my code so that it not only displays minutes! Because now i get for example: 80 minutes but 1 hour 20 minutes woud make more sense! Thanks

Upvotes: 1

Views: 1040

Answers (2)

Phlip
Phlip

Reputation: 5343

You are trying too hard; ActiveSupport in Rails makes this stuff easier.

(Time.now.minutes - f.wartezeit.minutes).to_s + ' minutes'

And, yes, Time is not a "time of day", it's a "moment in all of time" object.

Upvotes: 1

trh
trh

Reputation: 7339

patient. wartezeit is a Time element, a datetime element would look like Sun, 17 Nov 2013 08:11:08 -0700 - As for getting how it's displayed, I usually use a gem for that. Time and date parsing and subtraction is very easy to bungle, so for consistency I use the same thing.

https://github.com/abhidsm/time_diff

This will let you do

diff = Time.diff( f.wartezeit, Time.now, '%y, %M, %w, %d and %H %N %S') 
diff[:diff]

Would render something like

12 hours 20 minutes 0 seconds

Upvotes: 0

Related Questions