The seconds of a Time
object in Ruby are within the range 0–60. We can use the sec
method in Ruby to get the seconds of a Time
object.
t_o.sec
The sec
method takes no parameters. It is only invoked by the Time
object, which in this case is t_o
.
An integer value is returned that is within the range 0–60.
# create time objectst1 = Time.now # current timet2 = Time.at(946702800)t3 = Time.new(2002, 10, 31, 2, 2, 2)# get the minutes# and print resultsputs t1.secputs t2.secputs t3.secputs "#{t3.to_a}"
Time
object that has the current time value.Time.at()
method. This creates a Time
object that corresponds to the seconds that we pass to it since the Unix Epoch.Time.new()
. We pass the year, month, day of the month, hour of the day, minutes of the day and finally the seconds of the minute.sec
method on the Time
objects we created and print the results.t3
in the array format.