Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

time
ruby
subsec
communitycreator

How to return the subsecond of a Time object in Ruby

okwudili onyejiaku

Overview

We can use the subsec attribute to return the subsecond of a Time object in Ruby. Any Time object we create in Ruby has access to this attribute.

Syntax

time_object.subsec
Syntax to get subsecond of a Time object

Return value

The subsecond of the time_object is returned. The return value can also be a rational number.

Code example

# create time objects
time_object_one = Time.now
time_object_two = Time.new(2002)
time_object_three = Time.new(1999, 10, 02)

# get subseconds
# and print results
puts time_object_one.subsec
puts time_object_two.subsec
puts time_object_three.subsec
Code snippet to get subseconds of Time objects

Explanation

  • Line 2–3: We create some Time objects using the methods Time.now and Time.new().
  • Line 8–10: We use the subsec attribute on the Time objects we created and print their values to the console.

RELATED TAGS

time
ruby
subsec
communitycreator
RELATED COURSES

View all Courses

Keep Exploring