Parsing and Formatting
strftime and strptime.
Parsing and Formatting is a free Ruby Academy lesson on CoddyKit — lesson 2 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.
Formatting with strftime
strftime turns a Time or Date into a formatted string using format directives that start with %.
t = Time.new(2026, 5, 30, 14, 5, 9)
puts(t.strftime('%Y-%m-%d'))
puts(t.strftime('%H:%M:%S'))Common Date Directives
Useful date directives:
%Y4-digit year,%y2-digit%mmonth number,%Bfull month name%dday of month,%jday of year
t = Time.new(2026, 5, 30)
puts(t.strftime('%B %d, %Y'))
puts(t.strftime('%y/%m/%d'))Common Time Directives
Useful clock directives:
%H24-hour,%I12-hour%Mminutes,%Sseconds%pAM/PM
t = Time.new(2026, 5, 30, 14, 30)
puts(t.strftime('%I:%M %p'))
puts(t.strftime('%H:%M'))Weekday and Names
%A is the full weekday name, %a abbreviated. %b is the abbreviated month name.
t = Time.new(2026, 5, 30)
puts(t.strftime('%A'))
puts(t.strftime('%a %b %d'))Padding and Flags
Insert flags between % and the directive: - removes padding, 0 pads with zeros, _ pads with spaces.
t = Time.new(2026, 5, 5, 9, 0)
puts(t.strftime('%-d/%-m'))
puts(t.strftime('%H:%M'))ISO 8601 Output
For interoperable timestamps, use iso8601 or the %FT%T%z pattern. ISO 8601 is the standard for APIs.
require 'time'
t = Time.new(2026, 5, 30, 14, 0, 0, '+00:00')
puts(t.utc.iso8601)Parsing with Date.parse
Date.parse reads many common date string formats and guesses the layout. Convenient but lenient.
require 'date'
puts(Date.parse('2026-05-30'))
puts(Date.parse('30 May 2026'))Parsing with Time.parse
Require time to add Time.parse, which reads full timestamps including the clock.
require 'time'
t = Time.parse('2026-05-30 14:30:00')
puts(t.hour)
puts(t.min)Strict Parsing with strptime
When you know the exact format, use strptime. It uses the same directives as strftime and fails on mismatched input, which is safer.
require 'date'
d = Date.strptime('30/05/2026', '%d/%m/%Y')
puts(d.year)
puts(d.month)strptime for Times
Time.strptime parses a timestamp with a known format into a Time.
require 'time'
t = Time.strptime('2026-05-30 14:05', '%Y-%m-%d %H:%M')
puts(t.hour)
puts(t.min)parse vs strptime
Rule of thumb:
- Use
parsefor flexible, human-entered text - Use
strptimewhen the format is fixed and you want to reject anything else
require 'date'
puts(Date.parse('May 30 2026').iso8601)
puts(Date.strptime('2026-05-30', '%Y-%m-%d').iso8601)Quick Check
Which directive produces a zero-padded 24-hour value in strftime?
Recap: Parsing and Formatting
You can convert between strings and dates both ways:
strftimeformats with%directives%Y %m %d %H %M %S %A %Band flagsparseis flexible,strptimeis strict- Use
iso8601for API-friendly output
require 'date'
puts(Date.strptime('2026-05-30', '%Y-%m-%d').strftime('%A, %B %-d'))Frequently asked questions
Is the “Parsing and Formatting” lesson free?
Yes — the full text of “Parsing and Formatting” 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 “Parsing and Formatting”?
strftime and strptime. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Parsing and Formatting” 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