Parsing and Formatting
strftime and strptime.
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'))All lessons in this course
- Time and Date Classes
- Parsing and Formatting
- Time Arithmetic
- Time Zones