Time & Date in Ruby

Ruby includes Time and Date classes which can be used to initialize objects representing a given point in time or a given date.

# To get current point in time:
Time.now # => 2021-06-17 07:08:00 +0200

# To get current day:
Date.today # => Thu, 17 Jun 2021

# To get a humanly readable point in time at a given Unix time i.e. seconds since the Epoch (1970.01.01 00:00:00 UTC):
Time.at(1) # => 1970-01-01 01:00:01 +0100
Time.at(1623906931) # => 2021-06-17 07:15:31 +0200

# To get a humanly readable point in time using integers:
Time.new(2007, 1, 9, 12, 30) # => 2007-01-09 12:30:00 +0100
Date.new(2007, 1, 9) # => Tue, 09 Jan 2007

Time Zones

Each instance of Time class has a time zone included.

time_now = Time.now
time_now.zone # => "CEST"
Time.now.utc # => 2021-06-17 05:23:47 UTC

Formatting Time & Date

In Ruby representations of time & dates can be formatted using strftime method.

We use cookies and similar technologies to enhance the quality of services, maintain statistics and adjust marketing content. You will find more information in the Cookies Policy.

By clicking OK you grant consent to processing of your personal data by us and our Trusted Partners with the purpose of maintain statistics and adjustment of the marketing content pursuant to the Privacy Policy. If you wish to not grant that consent and/or limit its extent click Settings.