Dates & Times
Ruby handles moments in time with the built-in Time class and calendar days with the Date class from the standard library, both formatted with strftime .
Learn Dates & Times in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll read date parts, format with directives, parse strings, and do day and second arithmetic.
What You'll Learn in This Lesson
1️⃣ Time Basics & Reading Parts
Time.now gives the current instant; Time.new(...) builds a specific one. Pull out parts with year , month , day , hour , min , and wday (0 = Sunday). We pin the offset to UTC so the output is identical every run.
2️⃣ Formatting with strftime & Parsing
strftime turns a date or time into any string layout using % directives — %Y year, %m month, %d day, %A weekday name, and more. Going the other way, Date.parse reads a string back into a Date.
3️⃣ Date & Time Arithmetic
For a Date , + and - move by whole days, and subtracting two dates gives the gap between them. For a Time , the same operators move by seconds. Helpers like next_month handle calendar quirks for you.
Your turn. Count the days until a birthday and find which weekday it falls on. Fill in each ___ .
Mini-Challenge: Event Countdown
Print a schedule that shows each event's formatted date, its weekday, and how many days away it is from a fixed "today". This combines formatting and date math. Run with ruby schedule.rb .
Common Errors (and the fix)
- "uninitialized constant Date" — ❌ You forgot require 'date' . ✅ Add it (Time needs no require, Date does).
- Date difference looks weird — ❌ Subtracting dates yields a Rational. ✅ Call .to_i for whole days.
- Mixing up %m and %M — ❌ %m is month, %M is minutes. ✅ Lowercase = month, uppercase = minutes.
- Adding to a Time and expecting days — ❌ time + 1 adds one second . ✅ Add 86400 for a day, or use a Date.
Pro Tips
- 💡 ISO 8601: date.iso8601 gives the standard YYYY-MM-DD string for free.
- 💡 Remove padding: a hyphen like %-d drops the leading zero (so 5, not 05).
- 💡 Parsing is fuzzy: Date.parse is flexible but guesses; use Date.strptime(str, fmt) when the format is known.
📋 Quick Reference — Dates & Times
Task
Ruby
Now
Time.now / Date.today
Build a date
Date.new(2026, 6, 18)
Format
d.strftime("%Y-%m-%d")
Parse
Date.parse("2026-12-25")
Add days
date + 7
Days between
(b - a).to_i
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ Time is built in; Date needs require 'date'
- ✅ Read parts with year , month , day , wday
- ✅ Format with strftime directives
- ✅ Parse strings with Date.parse
- ✅ Date math = days; Time math = seconds
- ✅ Next lesson: JSON & YAML
Practice quiz
Which class requires before you can use it?
- Time
- Integer
- Date
- String
Answer: Date. Time is built in, but Date (and DateTime) live in the standard library and need require 'date'.
What does produce?
- A Date one week later (2026-06-25)
- A Date 7 months later
- A Time 7 seconds later
- An error
Answer: A Date one week later (2026-06-25). For a Date, + and - work in whole days, so +7 is one week later.
Subtracting two Date objects, e.g. , returns what kind of value?
- An Integer of days
- A Time
- A String
- A Rational number of days
Answer: A Rational number of days. Date subtraction yields a Rational number of days; call .to_i for a plain integer.
Adding to a Time, e.g. , moves it forward by how much?
- 3600 days
- 3600 seconds (one hour)
- 3600 minutes
- 3600 hours
Answer: 3600 seconds (one hour). Time arithmetic is in seconds, so + 3600 adds one hour.
Which strftime directive gives the full weekday name (e.g. 'Thursday')?
- %A
- %w
- %d
- %a
Answer: %A. %A is the full weekday name; %a is the abbreviated form and %d is the day of month.
What is the difference between %m and %M in strftime?
- No difference
- %m is minutes, %M is month
- %m is month, %M is minutes
- %m is military time
Answer: %m is month, %M is minutes. Lowercase %m is the month; uppercase %M is the minutes.
For , what does return (0 = Sunday)?
- 3
- 4
- 5
- 6
Answer: 4. June 18, 2026 is a Thursday, and wday counts from 0=Sunday, so Thursday is 4.
What does do?
- Formats a date into a string
- Returns the current date
- Raises an error
- Reads a string into a Date object
Answer: Reads a string into a Date object. Date.parse reads a string and returns a Date object.
Why did the lesson use fixed dates instead of Time.now?
- Time.now is deprecated
- So output is reproducible every run
- Time.now needs a require
- Fixed dates are faster
Answer: So output is reproducible every run. Time.now changes every run; fixed values keep example output identical and verifiable.
What does the directive prefix in %-d do compared with %d?
- Adds a leading zero
- Shows the weekday
- Removes the leading zero
- Uppercases the result
Answer: Removes the leading zero. A hyphen like %-d drops the zero-padding (5 instead of 05).
Continue this course
- Previous: Custom Exception Classes
- Next: JSON & YAML