Time and Date Classes
Working with time.
Time and Date Classes is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Time Class
Ruby's built-in Time class represents a moment in time. Time.now gives the current time including date and clock.
t = Time.now
puts t.class
puts(t.year)Building a Specific Time
Time.new (or Time.local) builds a time from parts: year, month, day, hour, minute, second.
t = Time.new(2026, 5, 30, 14, 30, 0)
puts t.year
puts t.hour
puts t.minReading Time Components
Time exposes each part through methods like year, month, day, hour, min, sec, and wday (day of week, 0 = Sunday).
t = Time.new(2026, 5, 30)
puts(t.month)
puts(t.day)
puts(t.wday)Epoch Seconds
Internally a Time is the number of seconds since the Unix epoch (Jan 1, 1970 UTC). Use to_i to get it and Time.at to convert back.
secs = Time.new(2026, 1, 1, 0, 0, 0, '+00:00').to_i
puts secs
puts(Time.at(secs).utc.year)The Date Class
For calendar work without a clock, require the date library and use the Date class.
require 'date'
d = Date.new(2026, 5, 30)
puts d.class
puts(d.to_s)Today and Now
Date.today gives the current calendar date. DateTime.now includes the time of day as well.
require 'date'
puts(Date.today.class)
puts(DateTime.now.class)Day of the Week
Date offers readable predicates and names for weekdays.
require 'date'
d = Date.new(2026, 5, 30)
puts(d.saturday?)
puts(d.wday)Date vs Time vs DateTime
Choose the right class:
Datecalendar date only, no clockTimeprecise instant with timezone, fastDateTimedate plus time, part of the date library
Modern Ruby code usually favors Time.
require 'date'
puts(Date.new(2026, 5, 30))
puts(Time.new(2026, 5, 30, 9, 0).hour)Leap Years
Date.leap? tells you whether a year is a leap year, which affects February's length.
require 'date'
puts(Date.leap?(2024))
puts(Date.leap?(2026))UTC and Local
A Time can be in local zone or UTC. utc converts to UTC, localtime back to local. utc? checks the current mode.
t = Time.new(2026, 5, 30, 12, 0, 0, '+03:00')
puts(t.utc.hour)
puts(t.utc?)Comparing Times
Time and Date objects are comparable with <, >, and ==, so you can sort them or check order directly.
a = Time.new(2026, 1, 1)
b = Time.new(2026, 6, 1)
puts(a < b)
puts([b, a].min.month)Quick Check
Which class represents a calendar date with no time-of-day component?
Recap: Time and Date Classes
You learned the core date and time types:
Time.now/Time.newfor instants- Components: year, month, day, hour, min, sec, wday
- Epoch seconds via
to_iandTime.at Date/DateTimefrom the date library- UTC vs local, comparison, and leap years
require 'date'
puts(Date.today.year)Frequently asked questions
Is the “Time and Date Classes” lesson free?
Yes — the full text of “Time and Date Classes” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Time and Date Classes”?
Working with time. 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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Time and Date Classes” 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