Home > Blockchain >  Rails: Converting created_at to PST
Rails: Converting created_at to PST

Time:06-03

I want to convert the created_at to this format January 12, 2:00 PM PST.

The first step I tried was to convert the created_at field to Pacific Standard Time (PST).

However, I'm stuck - I can't even get past this step.

I've tried these, but neither worked:

Time.parse(self.created_at).in_time_zone('Pacific Time (US & Canada)')

time = Time.parse(self.created_at)
time.in_time_zone('Pacific Time (US & Canada)')

I receive no implicit conversion of ActiveSupport::TimeWithZone into String when I do this.

I based it on these questions:

How do you convert the following time from UTC to EST in Ruby (without Rails)?

How to convert time from UTC to PST in rails

CodePudding user response:

Since it is already a datetime, you should be able to just do this to convert it:

self.created_at.in_time_zone('Pacific Time (US & Canada)')

Don't forget to save the new time zone to the record in the database. Time.parse() is meant to convert strings into datetime(https://apidock.com/ruby/v2_5_5/Time/parse/class).

  • Related