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 timeputs Time.now# create new time objecta = Time.now# print resultputs a
Code explanation
- Line 2: We print the current time.
- Line 5: We use the
Time.nowmethod to create a new time object. - Line 8: We print out the newly-created time object.