0Pricing
Ruby Academy · Lesson

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:

  • %Y 4-digit year, %y 2-digit
  • %m month number, %B full month name
  • %d day of month, %j day 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

  1. Time and Date Classes
  2. Parsing and Formatting
  3. Time Arithmetic
  4. Time Zones
← Back to Ruby Academy