How to get the current time in Ruby

Overview

In Ruby, we can use the Time.now method to get the current system time. We can also use it to create a new time object from the current system time.

Syntax

Time.now

Parameters

This method does not take any parameters.

Return value

The Time.now method returns the current system time when it is called.

Code example

# print current time
puts Time.now
# create new time object
a = Time.now
# print result
puts a

Code explanation

  • Line 2: We print the current time.
  • Line 5: We use the Time.now method to create a new time object.
  • Line 8: We print out the newly-created time object.

Free Resources