Time Arithmetic
Durations and differences.
Time Arithmetic is a free Ruby Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
Adding Seconds to a Time
Time arithmetic works in seconds. Adding a number to a Time moves it forward by that many seconds.
t = Time.new(2026, 5, 30, 12, 0, 0)
later = t + 60
puts(later.min)Subtracting Seconds
Subtracting a number moves a Time backward by that many seconds.
t = Time.new(2026, 5, 30, 12, 0, 0)
earlier = t - 3600
puts(earlier.hour)Readable Durations
Express durations as products of seconds so the intent is clear: 60 seconds, 60 * 60 an hour, 24 * 60 * 60 a day.
day = 24 * 60 * 60
t = Time.new(2026, 5, 30)
puts((t + day).day)Difference Between Two Times
Subtracting one Time from another gives a Float of seconds between them.
a = Time.new(2026, 5, 30, 12, 0, 0)
b = Time.new(2026, 5, 30, 13, 30, 0)
diff = b - a
puts(diff)
puts(diff / 60)Converting a Duration
Convert a raw seconds difference into hours and minutes using integer math.
secs = 5400
puts("#{secs / 3600}h #{(secs % 3600) / 60}m")Date Arithmetic in Days
Unlike Time, the Date class adds in days. Adding an integer moves forward by that many days.
require 'date'
d = Date.new(2026, 5, 30)
puts((d + 5).to_s)Difference Between Dates
Subtracting two Dates returns a Rational number of days. Call to_i for a whole-day count.
require 'date'
a = Date.new(2026, 1, 1)
b = Date.new(2026, 12, 31)
puts((b - a).to_i)Shifting Months and Years
The Date class has next_month, prev_month, next_year, and the >> / << operators to shift by months safely.
require 'date'
d = Date.new(2026, 5, 30)
puts(d.next_month.to_s)
puts((d >> 3).to_s)End-of-Month Safety
Month shifting clamps to valid dates. Adding a month to Jan 31 lands on the last valid February day, never an invalid date.
require 'date'
d = Date.new(2026, 1, 31)
puts((d >> 1).to_s)Counting Days Until
A common task: how many days until a future date. Subtract today from the target.
require 'date'
target = Date.new(2026, 12, 25)
from = Date.new(2026, 5, 30)
puts("#{(target - from).to_i} days to go")Measuring Elapsed Time
To measure how long code takes, capture Time.now before and after, then subtract.
start = Time.now
sum = 0
1000.times { |i| sum += i }
puts("sum=#{sum}")
puts((Time.now - start) >= 0)Quick Check
When you subtract one Time from another, what does Ruby return?
Recap: Time Arithmetic
You can now compute with time:
- Time adds/subtracts in seconds
- Date adds/subtracts in days
- Time minus Time gives seconds (Float)
- Date minus Date gives days (Rational)
next_month,>>,<<shift months safely
require 'date'
puts((Date.new(2026,6,1) - Date.new(2026,5,1)).to_i)Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “Time Arithmetic” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “Time Arithmetic”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
What will I learn in “Time Arithmetic”?
Durations and differences. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Time Arithmetic” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Time and Date Classes
- Parsing and Formatting
- Time Arithmetic
- Time Zones