Time & Date

Introduction

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.