[Ruby] Format a DateTime object as a string

Created on November 30, 2026

Call strftime with a format string. This example gets the current date and time and formats the year, month, and day with Japanese labels.

require 'date'

d = DateTime.now()
puts d.strftime('%Y年%m月%d日')

Example output

2021年11月24日

See the strftime format directives for more options.

Ruby