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 objects t1 = Time.now # current time t2 = Time.at(946702800) t3 = Time.new(2002, 10, 31, 2, 2, 2) # get the minutes # and print results puts t1.sec puts t2.sec puts t3.sec puts "#{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.RELATED TAGS
CONTRIBUTOR
View all Courses