How to get the minute of the hour of a time object in Ruby

Overview

The minute of an hour of a time object is from 0 - 59. One example is 5 minutes, 23 minutes, 59 minutes. etc.

To get this in Ruby, we have to call the min method on a time object.

Syntax

t_o.min
Get Minute of a Time Object in Ruby

Parameters

t_o: this is the time object we want to get its minutes.

Return value

An integer value from 1 - 59 is returned.

Code example

# create time objects
t1 = Time.now # current time
t2 = Time.new(946702800)
t3 = Time.new(2002, 10, 31, 2, 2, 2)
# get the minutes
# and print results
puts "#{t1.min} minutes"
puts "#{t2.min} minutes"
puts "#{t3.min} minutes"

Code explanation

  • Line 2: We created a time object that has a time value of now or current time.

  • Lines 3-4: We created time objects using the Time.new() method.

  • Lines 8-10: We called the min method on the time objects we created and printed the results.

Free Resources