Date
Added in v1.9 · Updated March 13, 2026 · Modules
ruby stdlib date calendar
The Date class provides methods for working with calendar dates in Ruby. It’s part of the standard library and offers powerful date manipulation and parsing capabilities.
Overview
Ruby’s Date class represents dates without times:
- Create: Generate dates from strings, components, or today
- Parse: Convert strings into Date objects
- Manipulate: Add, subtract, and calculate date differences
- Format: Display dates in various formats
Creating Date Objects
Today’s Date
Date.today # => #<Date: 2026-03-13>
Date.today.to_s # => "2026-03-13"
From String
require "date"
Date.parse("2026-03-13")
# => #<Date: 2026-03-13>
Date.parse("March 13, 2026")
# => #<Date: 2026-03-13>
From Components
Date.new(2026, 3, 13)
# => #<Date: 2026-03-13>
From Time
DateTime.now.to_date
Time.now.to_date
Date Components
You can extract individual components from a Date object:
date = Date.today
date.year # => 2026
date.month # => 3
date.day # => 13
date.wday # => 5 (0 = Sunday)
date.yday # => 72 (day of year)
date.leap? # => false
Date Calculations
Adding/Subtracting Days
date = Date.today
date + 1 # Tomorrow
date - 7 # One week ago
date + 30 # About one month later
Date Differences
date1 = Date.new(2026, 1, 1)
date2 = Date.new(2026, 3, 13)
(date2 - date1).to_i # => 71 days
Comparing Dates
past = Date.new(2020, 1, 1)
future = Date.new(2030, 1, 1)
future > past # => true
past <=> future # => -1 (past is earlier)
past == past # => true
Date Ranges
Iterating Over Dates
start = Date.new(2026, 3, 10)
finish = Date.new(2026, 3, 15)
(start..finish).each do |date|
puts date
end
# 2026-03-10
# 2026-03-11
# 2026-03-12
# 2026-03-13
# 2026-03-14
# 2026-03-15
Checking Range Inclusion
range = Date.new(2026, 1, 1)..Date.new(2026, 12, 31)
range.include?(Date.new(2026, 6, 15)) # => true
range.include?(Date.new(2027, 1, 1)) # => false
Date Formatting
strftime
The strftime method formats dates using directives:
date = Date.today
date.strftime("%Y-%m-%d") # => "2026-03-13"
date.strftime("%d/%m/%Y") # => "13/03/2026"
date.strftime("%B %d, %Y") # => "March 13, 2026"
date.strftime("%A") # => "Friday"
| Directive | Meaning | Example |
|---|---|---|
%Y | Year | 2026 |
%m | Month (01-12) | 03 |
%d | Day | 13 |
%A | Weekday | Friday |
%B | Month name | March |
Predefined Formats
date = Date.today
date.iso8601 # => "2026-03-13"
date.rfc2822 # => "Fri, 13 Mar 2026 00:00:00 +0000"
Date Calculations
Month Navigation
date = Date.new(2026, 3, 13)
date >> 1 # => 2026-04-13 (next month)
date << 1 # => 2026-02-13 (previous month)
Finding Day of Week
date = Date.new(2026, 3, 13)
date.monday? # => false
date.friday? # => true
date.sunday? # => false
Beginning/End of Month
date = Date.new(2026, 3, 13)
date.beginning_of_month # => 2026-03-01
date.end_of_month # => 2026-03-31
Parsing Dates
Flexible Parsing
require "date"
Date.parse("2026-03-13") # => 2026-03-13
Date.parse("03/13/2026") # => 2026-03-13
Date.parse("13-Mar-2026") # => 2026-03-13
With Format
require "date"
Date.strptime("13-03-2026", "%d-%m-%Y")
# => 2026-03-13
Practical Examples
Days Until Event
event = Date.new(2026, 12, 25)
today = Date.today
days_until = (event - today).to_i
puts "Days until Christmas: #{days_until}"
Week Number
date = Date.new(2026, 3, 13)
date.cweek # => 11 (ISO week number)
date.cwyear # => 2026 (ISO week year)
Day of Year
date = Date.new(2026, 3, 13)
date.yday # => 72 (day 72 of the year)