liveBook · Manning

Date

As explained in The Well-Grounded Rubyist, Fourth Edition

Date is a Ruby class provided by the date library for representing and manipulating calendar dates.

Date is a Ruby class from the date library for representing and manipulating calendar dates. Load it with require 'date'. Alongside Time and DateTime, it supports constructing, inspecting, formatting, converting, and moving through dates. section 8.4

Create dates from common inputs

Date.today returns the current date. Date.new(1959, 2, 1) constructs a date from numeric fields, while Date.parse("November 2 2013") accepts several human-readable forms. Date.parse also expands two-digit years: 03 becomes 2003, 33 becomes 2033, and 77 becomes 1977. section 8.4.1

Use Date objects to inspect and manipulate calendar values:

  • Query year, month, and day with methods such as year, month, and day; use predicates such as saturday? and leap? for calendar properties. section 8.4.2
  • Add or subtract days with + and -, shift months with << and >>, and move by named units with next_year, next_month, and prev_day. section 8.4.4
  • Iterate across a date range with upto or downto, and format dates for display instead of relying on their default representations. section 8.4.2 section 8.4.4
  • Convert Date to Time or DateTime when needed; missing time fields become zero, effectively midnight. section 8.4.4
>> d = Date.today
=>  #<Date: 2025-10-03 ((2460952j,0s,0n),+0s,2299161j)>
>> next_week = d + 7
=>  #<Date: 2025-10-10 ((2460959j,0s,0n),+0s,2299161j)>
>> d.upto(next_week) {|date| puts "#{date} is a #{date.strftime("%A")}" }
 2025-10-03 is a Friday
2025-10-04 is a Saturday
2025-10-05 is a Sunday
2025-10-06 is a Monday
2025-10-07 is a Tuesday
2025-10-08 is a Wednesday
2025-10-09 is a Thursday
2025-10-10 is a Friday

A one-week upto range prints each Date together with its weekday, showing calendar iteration in a compact Ruby session. section 8.4.4

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains constructors, parsing, queries, arithmetic, conversions, and iteration, while deferring specialized Julian and commercial forms.
Examples
Uses Date.today, Date.new, Date.parse, component queries, named shifts, and a one-week upto iteration.
What it assumes
Assumes basic Ruby objects and method calls, while teaching Date construction and manipulation from concrete irb-sized examples.
What you can do after
Apply it via Ruby's date library to create, query, format, convert, calculate, and iterate over calendar dates.
How it connects
Connects Date with Time, DateTime, date parsing, formatting, calendar arithmetic, and date ranges.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.