Ruby Date & Time
Working with dates, times, and timestamps
🕐 What are Date & Time in Ruby?
Ruby provides built-in classes for handling dates and times. The Time class works with timestamps, Date handles calendar dates, and DateTime combines both, making it easy to work with temporal data in your applications.
# Current time
now = Time.now
puts now # Output: 2025-01-07 14:30:45 +0000
# Current date
require 'date'
today = Date.today
puts today # Output: 2025-01-07
Output:
2025-01-07 14:30:45 +0000
2025-01-07
Date & Time Concepts
Time Class
Work with timestamps
Time.now
Time.new(2025, 1, 7)
Date Class
Handle calendar dates
Date.today
Date.new(2025, 12, 25)
Components
Extract date/time parts
time.year
time.month
time.day
Arithmetic
Add or subtract time
time + 3600
date + 7
🔹 Working with Time
The Time class represents a specific moment with date and time components. You can get the current time with Time.now or create specific times with Time.new. Time objects include hours, minutes, seconds, and timezone information for precise timestamp handling.
# Current time
current_time = Time.now
puts current_time # Output: 2025-01-07 14:30:45 +0000
# Create specific time
birthday = Time.new(2025, 12, 25, 10, 30, 0)
puts birthday # Output: 2025-12-25 10:30:00 +0000
# Access components
puts current_time.year # Output: 2025
puts current_time.month # Output: 1
puts current_time.day # Output: 7
puts current_time.hour # Output: 14
puts current_time.min # Output: 30
puts current_time.sec # Output: 45
Output:
2025-01-07 14:30:45 +0000
2025-12-25 10:30:00 +0000
2025
1
7
🔹 Working with Dates
The Date class focuses on calendar dates without time information. You need to require 'date' to use it. Date objects are perfect for birthdays, deadlines, or any scenario where you only need the day, month, and year without specific times.
require 'date'
# Current date
today = Date.today
puts today # Output: 2025-01-07
# Create specific date
new_year = Date.new(2025, 1, 1)
puts new_year # Output: 2025-01-01
# Access components
puts today.year # Output: 2025
puts today.month # Output: 1
puts today.day # Output: 7
# Day of week
puts today.wday # Output: 2 (0=Sunday, 1=Monday, etc.)
puts today.strftime("%A") # Output: Tuesday
Output:
2025-01-07
2025-01-01
2025
1
7
Tuesday
🔹 Formatting Dates and Times
Use strftime to format dates and times as strings with custom patterns. This method accepts format codes like %Y for year, %m for month, and %d for day. Formatting is essential for displaying dates in user-friendly ways or matching specific format requirements.
time = Time.new(2025, 1, 7, 14, 30, 45)
# Different formats
puts time.strftime("%Y-%m-%d") # Output: 2025-01-07
puts time.strftime("%d/%m/%Y") # Output: 07/01/2025
puts time.strftime("%B %d, %Y") # Output: January 07, 2025
puts time.strftime("%I:%M %p") # Output: 02:30 PM
puts time.strftime("%A, %B %d, %Y") # Output: Tuesday, January 07, 2025
# Date formatting
require 'date'
date = Date.today
puts date.strftime("%m/%d/%Y") # Output: 01/07/2025
Output:
2025-01-07
07/01/2025
January 07, 2025
02:30 PM
Tuesday, January 07, 2025
🔹 Date and Time Arithmetic
Perform calculations with dates and times using simple arithmetic. Add or subtract seconds from Time objects, or add/subtract days from Date objects. You can also calculate the difference between two dates or times to find durations or intervals.
# Time arithmetic (in seconds)
now = Time.now
one_hour_later = now + 3600 # Add 3600 seconds (1 hour)
one_day_ago = now - 86400 # Subtract 86400 seconds (1 day)
puts one_hour_later
puts one_day_ago
# Date arithmetic (in days)
require 'date'
today = Date.today
next_week = today + 7 # Add 7 days
last_month = today - 30 # Subtract 30 days
puts next_week # Output: 2025-01-14
puts last_month # Output: 2024-12-08
# Calculate difference
date1 = Date.new(2025, 1, 1)
date2 = Date.new(2025, 12, 31)
days_between = (date2 - date1).to_i
puts "Days: #{days_between}" # Output: Days: 364
Output:
2025-01-14
2024-12-08
Days: 364
🔹 Comparing Dates and Times
Compare dates and times using standard comparison operators like <, >, ==, and !=. This is useful for checking if a date has passed, if an event is upcoming, or sorting dates chronologically. Ruby makes temporal comparisons intuitive and straightforward.
require 'date'
date1 = Date.new(2025, 1, 1)
date2 = Date.new(2025, 12, 31)
today = Date.today
# Comparisons
puts date1 < date2 # Output: true
puts date1 == today # Output: false
puts today > date1 # Output: true
# Check if date is in the past
birthday = Date.new(2024, 6, 15)
if birthday < Date.today
puts "Birthday has passed" # Output: Birthday has passed
end
# Time comparisons
time1 = Time.new(2025, 1, 1, 10, 0, 0)
time2 = Time.new(2025, 1, 1, 15, 0, 0)
puts time1 < time2 # Output: true
Output:
true
false
true
Birthday has passed
true