Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

time
ruby
communitycreator

How to get the current time in Ruby

okwudili onyejiaku

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
Get the current time in Ruby

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.

RELATED TAGS

time
ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring