skip to content

In a Rails app, what does 2.weeks.ago return, and why can 1.day.from_now differ from 24.hours.from_now?

level: middleimportance: should knowfreq 50%

answer

  1. Integer becomes ActiveSupport::Duration
  2. ago and from_now default to Time.current
  3. days advance the calendar, hours count seconds
  4. a DST night is 23 or 25 hours
  5. Jan 31 + 1.month clamps to Feb 28

basics

~20 s

2.weeks is an ActiveSupport::Duration and .ago subtracts it from Time.current, giving a time two weeks back. Days, weeks, months and years move the calendar and keep the wall-clock time; hours move by exact seconds, so they diverge across DST.

solid answer

~40 s

`2.weeks` builds an `ActiveSupport::Duration` that remembers its parts (`weeks: 2`) as well as a value in seconds. `ago` and `from_now` (aliases `until` / `since`) apply it to `Time.current`, so in a Rails app, where `Time.zone` comes from `config.time_zone`, you get an `ActiveSupport::TimeWithZone`. Seconds, minutes and hours are added as exact seconds; days, weeks, months and years go through `advance` and move the calendar while keeping the wall-clock time. In a zone with daylight saving, `1.day.from_now` at 18:00 lands at 18:00 tomorrow, while `24.hours.from_now` can land at 17:00 or 19:00, even though `1.day == 24.hours` is true. Months clamp too: `Date.new(2026, 1, 31) + 1.month` is February 28.

code

ruby · 9 lines
ruby
Time.zone = "Europe/Berlin"
t = Time.zone.local(2026, 3, 28, 18, 0) # Saturday before DST starts

t + 1.day     # => 2026-03-29 18:00:00.000000000 CEST +02:00
t + 24.hours  # => 2026-03-29 19:00:00.000000000 CEST +02:00
1.day == 24.hours # => true (same value in seconds)

Date.new(2026, 1, 31) + 1.month # => Sat, 28 Feb 2026
1.month.to_i                    # => 2629746

go deeper

for a junior

Recall that 3.days.ago comes from Active Support, returns a time relative to Time.current, and that from_now and ago are the two directions.

for a middle

Explain that a Duration keeps its parts, that days and months go through advance while hours add seconds, and how month clamping works.

for a senior

Show you would pick calendar units for reminders and clock units for elapsed timeouts, and that a local config.time_zone exposes DST bugs that UTC hid.

for a principal

Decide where scheduling rules live: store local wall times plus a zone for user-facing events, and keep exact-second offsets for machine timeouts.

## Where `3.days` comes from Ruby's `Integer` has no `days` method. **Active Support** adds `seconds`, `minutes`, `hours`, `days`, `weeks` and `fortnights` to `Numeric`, and `months` and `years` to `Integer` only. Each returns an **`ActiveSupport::Duration`**, an object holding two things: - a **value** in seconds (`2.weeks.to_i` is `1209600`); - a hash of **parts** (`{weeks: 2}`), which is what makes calendar arithmetic possible. A Duration compares by value, so `1.day == 24.hours` is `true` and `1.day.to_i` is `86400`. For the variable units the value is an average: `1.month.to_i` is `2629746` (a twelfth of a Gregorian year) and `1.year.to_i` is `31556952`. ## Turning a Duration into a time | Method | Aliases | Default base | |---|---|---| | `since(time)` | `from_now`, `after` | `Time.current` | | `ago(time)` | `until`, `before` | `Time.current` | `Time.current` is `Time.zone.now` when a zone is set and `Time.now` otherwise. A Rails app always sets one from `config.time_zone` (default `"UTC"`), so `2.weeks.ago` returns an `ActiveSupport::TimeWithZone`. Adding a Duration to a `Time` or `Date` with `+` or `-` routes to the same code, so `event.starts_at - 2.weeks` and `2.weeks.before(event.starts_at)` are the same calculation. ## Calendar parts versus clock parts The Duration applies its parts one at a time: 1. **Seconds, minutes and hours** are added as an exact number of seconds. 2. **Days, weeks, months and years** go through `advance`, which changes the date and keeps the local wall-clock time. That difference only shows when the clock is not continuous. Take an RSVP reminder in a zone that observes daylight saving, say `Time.zone = "Europe/Berlin"`, scheduled from 18:00 on the Saturday before the clocks jump forward: - `t + 1.day` is **Sunday 18:00**: the calendar moved one day and the wall time stayed. - `t + 24.hours` is **Sunday 19:00**: exactly 86,400 seconds passed, and the night was only 23 hours long. In the default `UTC` zone the two agree, which is why the bug often appears only after someone sets `config.time_zone` to a local zone. Use calendar units for "same time tomorrow" and clock units for "exactly N hours later". ## Months, clamping and fractional units Calendar months have different lengths, so `advance` clamps to the last valid day: `Date.new(2026, 1, 31) + 1.month` is `2026-02-28`, not March. Adding `30.days` instead lands on March 2. A few more edge rules: - `1.5.days` works and is split into one day plus twelve hours. - `1.5.months` raises `NoMethodError`, because `months` and `years` exist on `Integer` only. - `Date + 1.day` returns a `Date`; adding an hour-based Duration to a `Date` returns a time. ## `beginning_of_day` and friends For due dates you usually want a boundary, not a moment. Active Support adds boundary methods to `Time`, `Date` and `TimeWithZone`: - `beginning_of_day` (aliases `midnight`, `at_midnight`) sets the time to 00:00; - `end_of_day` returns 23:59:59.999999999 on the same day, not midnight of the next; - `beginning_of_week` starts on the day set by `config.beginning_of_week` (`:monday` by default); - `end_of_month`, `next_occurring(:friday)` and similar helpers cover the rest. ```ruby class Event < ApplicationRecord def rsvp_due_at (starts_at - 2.weeks).end_of_day end def reminder_at rsvp_due_at.beginning_of_day + 9.hours end end ``` ## Durations elsewhere in Rails Because a Duration knows its size, Rails APIs accept one wherever a span of time is needed, so you rarely pass raw seconds: - cache entries take `expires_in: 12.hours`, and a job can be delayed with `set(wait: 1.day)`; - `2.weeks.inspect` is `"2 weeks"`, built from the parts, and `2.weeks.iso8601` is `"P2W"`; - `ActiveSupport::Duration.parse("P3D")` reads an ISO 8601 duration back, and `in_days`, `in_hours` and friends convert to a float. ## What interviewers check - That `3.days.ago` is Active Support, not Ruby, and returns a `TimeWithZone` in a Rails app. - That a Duration knows its parts, so day arithmetic respects the calendar. - That `1.month` is not a fixed 30 days, either as a value or when applied. - That `end_of_day` is the last instant of the day, which matters for inclusive date ranges.

  • Why does `1.day == 24.hours` return true if the two give different results across DST?
    `Duration#==` compares the values in seconds, and both are 86,400. The parts only matter when the Duration is applied to a time: `days` goes through `advance` and keeps the wall-clock time, while `hours` adds exact seconds. Equality and application answer different questions.
  • What does `(Date.new(2026, 1, 31) + 1.month) + 1.month` return, and why might that surprise a billing feature?
    February 28 first, then March 28: the first step clamps to February's last day and the second keeps the 28th. Chaining month steps drifts away from "the last day of each month". Compute each date from the original start, for example `start + n.months`, or use `end_of_month` when the rule is month-end.

A Duration made of days is like moving a meeting to "same time next Tuesday" in a paper diary: you turn pages and keep the hour. A Duration made of hours is a kitchen timer: it counts real seconds and ignores what the wall clock says.

  • 1.day.from_now and 24.hours.from_now always give the same time.
  • 1.month is always exactly 30 days when added to a date.
  • 3.days.ago is plain Ruby, because Integer has a days method.
  • end_of_day returns midnight at the start of the next day.
  • Date.new(2026, 1, 31) + 1.month raises because February 31 does not exist.