Rails/Date: Do not use `to_time` on Date objects

Viewed 2014

So I converted my ERB to Slim and getting hit with linter issues. I'm getting

Rails/Date: Do not use to_time on Date objects, because they know nothing about the time zone

The code in question is actually:

=@product.updated_at.to_time.strftime('%B %e @ %l:%M %p')

The data itself is actually: 2018-11-16 12:40:20.12345. My thought was to just try using .zone.today. Which results in

undefined method `today' for "EDT":String

So I thought I'll use .current/.current.strftime('%B %e @ %l:%M %p') and it results in

undefined method `current' for both attempts

I went into my application.rb and made sure I had:

config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local

What am I missing?

2 Answers

It seems like you should just do:

@product.updated_at.strftime('%B %e @ %l:%M %p')

If I do that on one of my records, I get:

"November 12 @  9:26 AM"

In some point of the work, I got some errors (like: "undefined method 'strftime' for \"2019-01-25T18:40:25.536Z\":String\nDid you mean? strip") using ...updated_at.strftime and some Rubocop warnings with to_time, so I solved my issues without warnings only with to_datetime.

Related