[Ruby] Display a DateTime with a different UTC offset

Created on November 30, 2026

DateTime#new_offset returns a new DateTime that represents the same instant with a different UTC offset. The example prints an instant first in UTC and then with the +09:00 offset used in Tokyo.

require 'date'

d = DateTime.iso8601('2021-11-27T13:05:23+00:00')
puts d.new_offset('+00:00').strftime('%Y-%m-%d %H:%M:%S')
puts d.new_offset('+09:00').strftime('%Y-%m-%d %H:%M:%S')
2021-11-27 13:05:23
2021-11-27 22:05:23

The argument is a fixed offset from UTC. To display the current time, start with DateTime.now.

Ruby