Home>
I want to solve

Model in Rails applicationcreated_atI want to find the date difference using.

What you can do now
lastday = Date.new (2020, 12, 31)
lastday --Date.today
=>(71/1)


As mentioned above, if the type is Date, you can find the number of days,create_atThe type may be different, so I can't find it.

What you can't do (what I want to achieve)

created_atUse to find the date difference
created_atTo the same type as Date (I think it can be calculated if the type is the same)

I would appreciate it if you could teach me.
I look forward to working with you.

  • Answer # 1

    Ruby time classes can be subtracted.
    Since you can see the number of seconds of the difference, I think you should divide it by the number of seconds in a day (1.days is OK).

    Time.zone.now --Model.first.created_at
    =>6949.569539191
    past_seconds = Time.zone.now --Model.first.created_at
    past_days = (past_seconds/1.days) .floor