Change The Time Zone Offset Of A DateTime Object
Let's say you have a timestamp string that you parse with DateTime.parse.
> DateTime.parse('2021-02-23T11:59:11')
#=> Tue, 23 Feb 2021 11:59:11 +0000Without the specification of a time zone offset in the timestamp string, it will be parsed as UTC.
If you want to change it to another time zone, you can alter the offset option of the DateTime object. Rails provides the #change method for doing this.
> DateTime.parse('2021-02-23T11:59:11').change(offset: '-600')
#=> Tue, 23 Feb 2021 11:59:11 -0600By changing the offset to -600, the DateTime now represents a time in Central Time.
Last updated
Was this helpful?