简体   繁体   中英

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

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?

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"

在工作的某些方面,我使用...updated_at.strftime遇到了一些错误(例如:“ \\“ 2019-01-25T18:40:25.536Z \\”的未定义方法'strftime':String \\ n这是不是意思?”) ...updated_at.strftime和带有to_time一些Rubocop警告,所以我仅通过to_datetime解决了没有警告的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM