Dates and Times

Time zones: use the tzinfo gem or ActiveSupport (Time.zone, in_time_zone). Store times in UTC.

Common strftime codes: %Y %m %d %H %M %S %L (ms) %z %Z %A %a %B %b %j %p %I %e %s %F (= %Y-%m-%d) %T (= %H:%M:%S).

now = Time.now
utc = Time.now.utc
t = Time.new(2025, 10, 24, 14, 30, 0, "+05:30")
now.year; now.month; now.day; now.hour; now.wday; now.yday; now.zone
now + 3600                          # one hour later (seconds)
(Time.now - t) / 86400              # days between
now.strftime("%Y-%m-%d %H:%M:%S %z")   # "2025-10-24 14:30:00 +0530"
now.strftime("%A, %d %B %Y")
now.iso8601                          # require "time"
Time.parse("2025-10-24 10:00")       # require "time"
Time.at(1_700_000_000)               # from epoch
now.to_i                             # epoch seconds
Time.new("2025-10-24 10:00:00")      # string parsing in Time.new (Ruby 3.3+)

require "date"
d = Date.today
Date.new(2025, 12, 25) - d           # Rational days
d.next_day(7); d.prev_month; d >> 1; d.saturday?; d.leap?; d.cweek
Date.parse("24 Oct 2025"); Date.iso8601("2025-10-24"); Date.valid_date?(2025, 2, 30)
(d..d + 6).map { it.strftime("%a") }   # ["Fri", "Sat", ...]
DateTime.now                         # deprecated in favor of Time

Process.clock_gettime(Process::CLOCK_MONOTONIC)   # for measuring durations