Subtract one Date from the other and convert the result: (finish - start).to_i is the number of days. The subtraction alone returns a Rational such as (45/1), which is why the to_i matters. For times, Time - Time returns the seconds between them as a Float, and Time.at(1_700_000_000).utc turns a Unix timestamp into a Time.
Ruby has two classes for this job: Date (a calendar day, from require 'date') and Time (an instant, down to fractions of a second). Subtracting two of the same class gives the difference, but in different units: days as a Rational for Date, seconds as a Float for Time. There is no timedelta or TimeSpan object as in Python or C#; you get a number and do the arithmetic. The sections below cover days between dates, elapsed time and a "3 days 5 hours" breakdown, Unix timestamps, months and years, business days, and moving old DateTime code to Time, then a table to pick from. Each example runs on this page: hit Run, then edit the code and run it again.
1Days between two dates: Date minus DateRecommended
Build each day with Date.new(year, month, day), or parse a string with Date.iso8601 (strict, YYYY-MM-DD) or Date.parse (lenient), then subtract. Date#- returns a Rational, an exact fraction of days, so call to_i before you print or compare it as a count. The sign follows the order: later minus earlier is positive, and abs drops the sign when you only want the distance. Leap days are part of the calendar math, so nothing extra is needed for February.
require 'date' start = Date.new(2026, 1, 15) finish = Date.new(2026, 3, 1) diff = finish - start p diff p diff.class puts "#{diff} days" puts "#{diff.to_i} days" # Parse strings first; ISO 8601 (YYYY-MM-DD) is the safe format christmas = Date.iso8601("2026-12-25") today = Date.parse("2026-12-02") to_go = (christmas - today).to_i puts "#{to_go} days to go" ago = (today - christmas).to_i p ago p ago.abs # Leap years are counted for you leap = (Date.new(2024, 3, 1) - Date.new(2024, 2, 28)).to_i normal = (Date.new(2026, 3, 1) - Date.new(2026, 2, 28)).to_i puts "2024: #{leap} days, 2026: #{normal} day" p Date.leap?(2024) p Date.leap?(2100)
Output
p diff shows (45/1) and its class Rational, and interpolating it without to_i prints 45/1 days, the bug most people hit first. to_i gives 45 days. Subtracting in the other order gives -23, and abs turns it back into 23. Between February 28 and March 1 there are 2 days in 2024 and 1 in 2026, and 2100 is not a leap year (false). Beware that Date.parse("03/04/2026") reads the string as day/month and gives April 3; for a US-style string use Date.strptime(s, "%m/%d/%Y"), which gives March 4.
2Elapsed time: Time minus Time, then divmod
When the time of day matters, use Time. Time - Time returns the seconds between the two instants as a Float; divide by 3600 for hours or 86_400 for days. To show the gap as "3 days 5 hours", peel units off with divmod, which returns the quotient and the remainder in one call. Each Time carries its UTC offset, so times in different offsets subtract correctly. To parse an ISO 8601 timestamp, add require 'time': without it, Time.iso8601 raises NoMethodError: undefined method 'iso8601' for class Time.
require 'time' start = Time.utc(2026, 12, 1, 9, 30, 0) finish = Time.utc(2026, 12, 4, 14, 45, 10) seconds = finish - start p seconds p seconds.class puts "#{(seconds / 3600).round(2)} hours" puts "#{(seconds / 86_400).floor} whole days" # Break the seconds into days, hours, minutes and seconds def breakdown(seconds) mins, secs = seconds.to_i.divmod(60) hours, mins = mins.divmod(60) days, hours = hours.divmod(24) "#{days} days #{hours} hours #{mins} minutes #{secs} seconds" end puts breakdown(seconds) # Each Time keeps its offset, so the math is exact tokyo = Time.new(2026, 7, 1, 9, 0, 0, "+09:00") london = Time.new(2026, 7, 1, 9, 0, 0, "+01:00") puts "#{(london - tokyo) / 3600} hours apart" # ISO 8601 strings (needs require 'time') sent = Time.iso8601("2026-12-02T08:00:00Z") read = Time.iso8601("2026-12-02T10:30:00+02:00") p read p read - sent
Output
The gap is 278110.0 seconds, a Float: about 77.25 hours or 3 whole days, and breakdown spells it out as 3 days 5 hours 15 minutes 10 seconds. 9:00 in Tokyo is 8 hours before 9:00 in London, so the difference is 8.0 hours apart. The message was sent at 08:00 UTC and read at 10:30 at +0200 (08:30 UTC), which is 1800.0 seconds later. Because Time subtraction counts real seconds, a local day that crosses a daylight-saving change can be 23 or 25 hours long; for calendar days, convert with to_date and use section 1.
3Unix timestamps: Time.at and to_i
A Unix timestamp is the number of seconds since 1970-01-01 00:00 UTC. Time.at(seconds) turns it into a Time in the machine's local zone; add .utc, or pass in: "+09:00" for a fixed offset, to get the same result on every machine. Timestamps in milliseconds (what JavaScript's Date.now() returns) take a unit argument instead of a division by 1000.0. Going the other way, to_i on any Time gives its timestamp. The difference between two timestamps is plain integer math.
require 'date' require 'time' # Seconds since 1970-01-01 UTC to a Time t = Time.at(1_700_000_000).utc p t puts t.iso8601 puts t.strftime("%Y-%m-%d %H:%M") puts t.to_date # The same instant at fixed offsets puts Time.at(1_700_000_000, in: "+09:00") puts Time.at(1_700_000_000, in: "-05:00") # Milliseconds: pass the unit instead of dividing by 1000.0 ms = 1_700_000_000_123 puts Time.at(0, ms, :millisecond, in: "UTC").strftime("%F %T.%L") p Time.at(ms / 1000.0).utc # And back: a Time to a Unix timestamp p Time.utc(2026, 12, 2).to_i p Time.iso8601("2026-12-02T09:00:00+09:00").to_i # The current timestamp changes every run, so check its shape now = Time.now.to_i p now.class p now > 1_700_000_000 p Time.now.utc.utc? # Two timestamps: subtract, then divide gap = 1_700_086_400 - 1_700_000_000 puts "#{gap} seconds = #{gap / 86_400} day"
Output
1_700_000_000 is 2023-11-14 22:13:20 UTC, which is 2023-11-15 07:13:20 +0900 at +09:00 and 2023-11-14 17:13:20 -0500 at -05:00. The millisecond line keeps .123 exactly, while the division by 1000.0 goes through a Float and prints a fraction like 515899/4194304. The last two timestamps are the same 1796169600: midnight UTC and 09:00 at +09:00 are one instant. Time.now.to_i is the UTC timestamp whatever the local zone is, so it needs no conversion; Time.now.utc gives the current time as a UTC Time. Without .utc, Time.at prints this machine's zone, +0000 here and your own zone on your computer.
4Weeks, months and years between two dates
Weeks are days divided by 7, and divmod(7) keeps the leftover days. Months and years are not a fixed number of days, so count calendar months: the difference in years times 12 plus the difference in months, minus one if the last month is not complete yet. Date#>> adds months (and << subtracts them), clamping to the end of a shorter month, which makes it the right test for "has the month passed?". Whole years, such as an age, are whole months divided by 12.
require 'date' # Whole calendar months from one date to a later one def months_between(from, to) months = (to.year - from.year) * 12 + (to.month - from.month) months -= 1 if (from >> months) > to months end def years_between(from, to) months_between(from, to) / 12 end p months_between(Date.new(2026, 1, 15), Date.new(2026, 3, 15)) p months_between(Date.new(2026, 1, 15), Date.new(2026, 3, 14)) p months_between(Date.new(2026, 1, 31), Date.new(2026, 2, 28)) # An age: has this year's birthday passed? birthday = Date.new(1990, 12, 3) p years_between(birthday, Date.new(2026, 12, 2)) p years_between(birthday, Date.new(2026, 12, 3)) p years_between(Date.new(2024, 2, 29), Date.new(2025, 2, 28)) # Weeks and leftover days start = Date.new(2026, 1, 31) finish = Date.new(2026, 12, 2) days = (finish - start).to_i weeks, rest = days.divmod(7) puts "#{days} days = #{weeks} weeks and #{rest} days" # >> and << move by months; + and - move by days puts start >> 1 puts start << 1 puts start + 30
Output
January 15 to March 15 is 2 months, and one day short of that is 1. January 31 to February 28 counts as 1 month because start >> 1 lands on 2026-02-28, the last day of February. The age is 35 the day before the birthday and 36 on it, and someone born on February 29 turns 1 on February 28 of the next year by this rule (some laws say March 1; pick the rule your domain needs). The gap prints as 305 days = 43 weeks and 4 days. Compare start >> 1 (2026-02-28) with start + 30 (2026-03-02): a month is not 30 days.
5Business days: a date range and count
A range of dates is enumerable, so (start..finish).count with a block counts the days that pass a test. saturday? and sunday? name the weekend, and wday numbers the weekdays from 0 (Sunday) to 6. Put the holidays in an array or a Set and skip those too. .. includes the last date and ... leaves it out; decide which one your rule means. For a long span, counting day by day is still fast: a year is only 365 checks.
require 'date' start = Date.new(2026, 12, 1) finish = Date.new(2026, 12, 31) # Every date in the range, weekends left out weekdays = (start..finish).count { |d| !d.saturday? && !d.sunday? } p weekdays # ... leaves the last date out p (start...finish).count { |d| !d.saturday? && !d.sunday? } # wday: 0 is Sunday, 1..5 are Monday to Friday holidays = [Date.new(2026, 12, 24), Date.new(2026, 12, 25)] business = (start..finish).count do |d| (1..5).cover?(d.wday) && !holidays.include?(d) end puts "#{business} business days" # Other questions about the same range p (start..finish).count(&:sunday?) p (start..finish).select(&:monday?).map(&:day) puts start.strftime("%A")
Output
December 2026 has 23 weekdays, 22 when the range stops before the 31st, and 21 business days once the two holidays (a Thursday and a Friday) are taken out. The same range answers other questions: it has 4 Sundays, its Mondays fall on [7, 14, 21, 28], and it starts on a Tuesday. Rails adds helpers such as on_weekday? through ActiveSupport; plain Ruby has the wday and day-name methods shown here.
6DateTime vs Time: move old code to Time
Many older answers use DateTime, a subclass of Date that adds a time of day. Ruby's own documentation now calls DateTime deprecated and says to use Time; it still works and prints no warning, so old code keeps running. Its subtraction returns a Rational number of days, as Date does, so differences come out as fractions like 27811/8640. Call to_time on each side to get seconds instead, and to_date on a Time when you need calendar days. You cannot subtract a Time from a Date directly; convert one side first.
require 'date' require 'time' # DateTime still works, but differences are fractions of a day a = DateTime.new(2026, 12, 1, 9, 30, 0) b = DateTime.new(2026, 12, 4, 14, 45, 10) diff = b - a p diff p diff.class seconds = (diff * 86_400).to_i p seconds # Convert both to Time and the difference is in seconds p b.to_time - a.to_time # Old code that parses into DateTime: same move sent = DateTime.parse("2026-12-02T10:00:00+02:00") p sent.to_time p sent.to_time.utc # to_date keeps the calendar day of the time's own offset late = Time.new(2026, 12, 2, 23, 0, 0, "-05:00") puts late.to_date puts late.utc.to_date # Date and Time don't mix in subtraction begin Date.new(2026, 12, 2) - Time.utc(2026, 12, 1) rescue TypeError => e puts "TypeError: #{e.message}" end
Output
The DateTime difference is (27811/8640) days, which is 278110 seconds once multiplied out, the same 278110.0 that the to_time version returns directly. The parsed string keeps its offset (2026-12-02 10:00:00 +0200) until you call utc. Late evening at -05:00 is still December 2 there but already December 3 in UTC, so choose the offset before you call to_date. Mixing the classes fails with TypeError: expected numeric: Date#- accepts a Date or a number of days, not a Time.
7Which should you use?
| Method | Returns | Best for |
|---|---|---|
| (d2 - d1).to_i | Integer days | Days between two calendar dates: most questions |
| t2 - t1 | Float seconds | Elapsed time in seconds, minutes or hours |
| secs.divmod(60) … | [quotient, rest] | A "3 days 5 hours" breakdown |
| Time.at(ts).utc | Time | Unix timestamp to a date; to_i goes back |
| from >> n | Date | Whole months and years, ages |
| (d1..d2).count { … } | Integer | Business days, or any days that pass a test |
| dt2 - dt1 (DateTime) | Rational days | Old code only: convert with to_time |
Frequently asked questions
Why does subtracting two dates print 45/1 instead of 45?
Date#- returns a Rational, an exact fraction of days, and a Rational prints as 45/1 in a string (p shows (45/1)). Call to_i for a whole number of days: (finish - start).to_i is 45. DateTime subtraction returns a Rational too, with real fractions such as 27811/8640 when the times of day differ.
How do I count the days from today to a date?
Write require 'date', then (target - Date.today).to_i. The result is negative once the date has passed; add .abs for the distance either way. Date.today uses the local time zone of the machine running the code; the clock on this page is UTC. Without the require, Date is unknown and the line fails with NameError: uninitialized constant Date.
How do I get the current UTC timestamp in Ruby?
Time.now.to_i is the Unix timestamp in seconds, and it is the same whatever the local time zone is, so no conversion is needed. Time.now.utc gives the current time as a UTC Time object. For milliseconds, use Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond), which returns an Integer, or (Time.now.to_r * 1000).to_i.
Should I use DateTime or Time?
Use Time for moments and Date for calendar days. Ruby's documentation calls DateTime deprecated and recommends Time. DateTime still works without a warning, so old code runs, but its differences come out as fractions of a day. Convert with to_time (and back to a day with to_date) when you meet it.
How do I work with named time zones like America/New_York?
Plain Ruby Time knows fixed offsets such as "+09:00" and the zone of the process, which you can set with ENV['TZ'] on a system that has a time-zone database. For conversions between named zones, use the tzinfo gem (Rails wraps it as in_time_zone). This page has no time-zone database, so named zones are ignored and times stay in UTC; the examples use UTC and fixed offsets.
Does Time subtraction account for daylight saving time?
Yes: t2 - t1 counts the real seconds between two instants, so a local day that crosses a daylight-saving change is 23 or 25 hours long. That is right for elapsed time but wrong for counting calendar days. For days, convert both sides with to_date and subtract the dates.
Is there a Ruby helper like Rails' 3.days.ago or time_ago_in_words?
Not in plain Ruby: 3.days.ago comes from ActiveSupport and time_ago_in_words from Rails view helpers. Without Rails, subtract days from a Date (Date.new(2026, 12, 2) - 3), seconds from a Time (t - 3 * 86_400), and build the words yourself with divmod, as in the breakdown in section 2.