2017-03-10 6 views
0

配列が作成された時間と現在の時間の差を取得しようとしています。これは完全に論理的だと思われますが、それは今まで働いていないようです。Ruby、配列が作成された時間と現在の時刻の差

mergelisting = MergeList.Pluck(:created_at, :partner, :gambler, :amount) 
    (mergelisting).each do |i| 
    t = Time.now 
    nowtime = t.strftime("%I") 
    nowtime.to_i 
    mergetime = i[1].strftime("%I") 
    mergetime.to_i 
    if nowtime - mergetime == 6 
     do some stuff... 
    end 
    end 

私の本当の問題は、24と6の間の時間の差が6時間であるが、24-6 6.私はここで何を行うことができます

+0

これは大変複雑です。 Railsを使用しているので、http://stackoverflow.com/questions/6784527/calculating-difference-in-time-between-two-time-objectsとhttp://apidock.com/rails/ActiveSupport/を参照することをお勧めします。 TimeWithZone /%3Fの間。 – anothermh

+0

mergetimeとnowtimeの違いが6時間のときは、何かしてください... – Mordred

答えて

0

を明らかにしなかった私は、MergeList.Pluck返しますArrayを想定していますこの配列の最初の項目は、データベースのRails created_atの列になります。次に:

mergelisting = MergeList.Pluck(:created_at, :partner, :gambler, :amount) 

mergetime = mergelisting.first   # Time from DB (created_at) 
nowtime = Time.now      # Current time 
timediff_s = nowtime - mergetime   # Difference in seconds 
timediff_h = timediff_s/(60 * 60).to_f # Difference in hours as a floating point number 

# 
# Not exactly sure if this is what you wanted, but you now 
# have the time difference as a floating point number expressed 
# in hours. So you can do whatever you want with it 
# 
if (timediff_h.to_i == 6) 
    ...the difference is anywhere between 6.0 and 7.0 hours... 
    ...do whatever you need to do... 
end 
関連する問題