Home » Ruby Date & Time Tutorial (with Code Examples)

Ruby Date & Time Tutorial (with Code Examples)

Ruby makes working with dates and times pleasantly straightforward once you know which class to reach for:

  • Date (calendar dates, no time of day)
  • Time (time of day + timezone/offset, great for “now” and timestamps)
  • DateTime (date + time + offset, more “calendar-like” than Time)

You’ll use these built-ins most of the time:

require "date" # needed for Date and DateTime

1) Getting the Current Date and Time

Current time (Time)

now = Time.now
puts now
# => 2025-12-12 10:15:30 +0000 (example)

Current date (Date)

today = Date.today
puts today
# => 2025-12-12 (example)

Current date & time (DateTime)

dt = DateTime.now
puts dt
# => 2025-12-12T10:15:30+00:00 (example)

2) Creating Specific Dates and Times

Create a Date

d = Date.new(2025, 12, 25)
puts d
# => 2025-12-25

Parse a date string

d1 = Date.parse("2025-12-25")
d2 = Date.strptime("25/12/2025", "%d/%m/%Y")

puts d1
puts d2
# => 2025-12-25
# => 2025-12-25

Create a Time

t = Time.new(2025, 12, 25, 14, 30, 0, "+00:00")
puts t
# => 2025-12-25 14:30:00 +0000

Parse a Time (ISO 8601)

require "time"

t = Time.iso8601("2025-12-25T14:30:00+00:00")
puts t
# => 2025-12-25 14:30:00 +0000

3) Formatting Dates and Times

Date#strftime

d = Date.new(2025, 12, 25)
puts d.strftime("%A, %d %B %Y")
# => Thursday, 25 December 2025

Time#strftime

t = Time.new(2025, 12, 25, 14, 30, 0, "+00:00")
puts t.strftime("%Y-%m-%d %H:%M:%S %z")
# => 2025-12-25 14:30:00 +0000

Common format tokens:

  • %Y year, %m month, %d day
  • %H hour (24h), %M minute, %S second
  • %z timezone offset, %A weekday name, %B month name

4) Date/Time Arithmetic (Add/Subtract)

Add days to a Date

d = Date.new(2025, 12, 25)
puts d + 7
# => 2026-01-01

Subtract dates (difference in days)

a = Date.new(2025, 12, 25)
b = Date.new(2025, 12, 12)

puts (a - b).to_i
# => 13

Add seconds/minutes/hours to a Time

t = Time.now
puts t + 60        # +1 minute
puts t + 3600      # +1 hour
puts t - 86400     # -1 day

5) Comparing Dates and Times

a = Date.new(2025, 12, 25)
b = Date.new(2025, 12, 12)

puts a > b   # true
puts a == b  # false

For Time:

t1 = Time.now
sleep 1
t2 = Time.now

puts t2 > t1  # true

6) Converting Between Date, Time, and DateTime

Date → Time (start of day, local)

d = Date.new(2025, 12, 25)
t = d.to_time
puts t

Time → Date

t = Time.now
puts t.to_date

Time → DateTime

require "date"
t = Time.now
puts t.to_datetime

Tip: conversions can be affected by timezone/offset and your environment.

7) Working With Timezones (Practical Basics)

Ruby’s built-in Time stores an offset (like +00:00) and can show local vs UTC.

UTC vs local

t = Time.now
puts t
puts t.utc

Force a specific offset

t = Time.new(2025, 12, 25, 14, 30, 0, "+02:00")
puts t
# => 2025-12-25 14:30:00 +0200

If you need full IANA timezone support (e.g., Europe/London) in plain Ruby, people often use tzinfo or Rails’ ActiveSupport::TimeZone.

8) Common Real-World Tasks

8.1) Validate a date string safely

require "date"

def valid_date?(str, fmt = "%Y-%m-%d")
  Date.strptime(str, fmt)
  true
rescue ArgumentError
  false
end

puts valid_date?("2025-12-25")  # true
puts valid_date?("2025-13-25")  # false

8.2) Measure elapsed time (benchmark style)

start = Time.now
# ... code you want to measure ...
sleep 0.3
elapsed = Time.now - start
puts "Elapsed: #{elapsed.round(3)}s"

8.3) “Start of day” and “end of day” (without Rails)

require "date"

d = Date.today
start_of_day = Time.new(d.year, d.month, d.day, 0, 0, 0, "+00:00")
end_of_day   = Time.new(d.year, d.month, d.day, 23, 59, 59, "+00:00")

puts start_of_day
puts end_of_day

 

FAQ

Q: What’s the difference between Date and Time in Ruby? A: Date represents calendar dates without time-of-day. Time includes time-of-day and timezone/offset, and is best for timestamps and “now”.

Q: How do I format a date in Ruby? A: Use strftime, e.g. Date.today.strftime("%Y-%m-%d").

Q: How do I parse a date safely in Ruby? A: Use Date.strptime inside a begin/rescue (or helper method) to handle invalid inputs.

Q: How do I add days to a date in Ruby? A: Date.new(2025,12,25) + 7 returns a new Date 7 days later.

You may also like