Date arithmetic can add days and compare two fixed calendar dates.

date arithmetic Adding an integer to a `Date` moves forward by that many days.

Duration Calculations

days_to_add
duration_calculations.rb
Replay: real traced execution (multi-file project)
require "date"

days_to_add = 10
start_date = Date.new(2025, 5, 1)
due_date = start_date + days_to_add

span = (due_date - start_date).to_i

puts "start=#{start_date.iso8601}"
puts "due=#{due_date.iso8601}"
puts "span_days=#{span}"
require "date"

days_to_add = 1
start_date = Date.new(2025, 5, 1)
due_date = start_date + days_to_add

span = (due_date - start_date).to_i

puts "start=#{start_date.iso8601}"
puts "due=#{due_date.iso8601}"
puts "span_days=#{span}"
require "date"

days_to_add = 30
start_date = Date.new(2025, 5, 1)
due_date = start_date + days_to_add

span = (due_date - start_date).to_i

puts "start=#{start_date.iso8601}"
puts "due=#{due_date.iso8601}"
puts "span_days=#{span}"
  1. days_to_add ← 10, start_date ← 2025-05-01, due_date ← 2025-05-11

    1require "date"23days_to_add→ 10 = 10  #@days_to_add=1, 304start_date→ 2025-05-01 = Date.new(2025, 5, 1)5due_date→ 2025-05-11 = start_date2025-05-01 + days_to_add1067span→ 10 = (due_date2025-05-11 - start_date2025-05-01).to_i89puts "start=#{start_date.iso86012025-05-01}"10puts "due=#{due_date.iso86012025-05-11}"11puts "span_days=#{span10}"
    outputstart=2025-05-01
    due=2025-05-11
    span_days=10
  1. days_to_add ← 1, start_date ← 2025-05-01, due_date ← 2025-05-02

    1require "date"23days_to_add→ 1 = 14start_date→ 2025-05-01 = Date.new(2025, 5, 1)5due_date→ 2025-05-02 = start_date2025-05-01 + days_to_add167span→ 1 = (due_date2025-05-02 - start_date2025-05-01).to_i89puts "start=#{start_date.iso86012025-05-01}"10puts "due=#{due_date.iso86012025-05-02}"11puts "span_days=#{span1}"
    outputstart=2025-05-01
    due=2025-05-02
    span_days=1
  1. days_to_add ← 30, start_date ← 2025-05-01, due_date ← 2025-05-31

    1require "date"23days_to_add→ 30 = 304start_date→ 2025-05-01 = Date.new(2025, 5, 1)5due_date→ 2025-05-31 = start_date2025-05-01 + days_to_add3067span→ 30 = (due_date2025-05-31 - start_date2025-05-01).to_i89puts "start=#{start_date.iso86012025-05-01}"10puts "due=#{due_date.iso86012025-05-31}"11puts "span_days=#{span30}"
    outputstart=2025-05-01
    due=2025-05-31
    span_days=30