Search code examples
ruby-on-railsrubyactivesupport

Find the difference between a Time and 11 am on that day


I have an ActiveSuppport::TimeWithZone object and I want to find out how many minutes after 11 AM on that day it is. So, for example, if the time is 11:47 AM, I want the answer to be 47. Is there a way in Ruby/Rails to do this. Thanks!


Solution

  • Subtracting one Time object from another gives the difference in seconds. Just divide by 60 to get minutes.

    (mytime - Time.parse('11 AM')) / 60