Is there a rails way to convert some ActiveSupport::TimeZone object e.g. 2021-01-15 04:32:30 UTC
into a more human friendly time like 2021-01-15 04:32:30 PM
?
It would be easy to manipulate the string and work out if it's after/before midday, but I just want to check that there's not already something out there that does this
You can use strftime convert time object in required format.
Time.now.strftime("%Y-%m-%d %H:%M:%S %p")
=> "2020-10-27 13:49:35 PM"
Rails provides to_formatted_s method for Time
object, you can set your custom format in config/initializers/time_formats.rb
in config/initializers/time_formats.rb
Time::DATE_FORMATS[:custom_date_format] = lambda { |time| time.strftime("%Y-%m-%d %H:%M:%S %p") }
And use it like
Time.now.to_formatted_s(:custom_date_format)
OR
Time.now.to_s(:custom_date_format)