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 ___ .
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 .
📋 Quick Reference — Dates & Times
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).