Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rails: Converting created_at to PST

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading