wenincode
wenincode

Reputation: 396

How to calculate days passed since date

I have a record that has a field created_at with the following format: created_at: Tue, 26 Jan 2016 23:31:27 UTC +00:00.

I can compare it against specific days doing something like: created_at >= 5.days.ago.

Is there a simple way to calculate how many days have passed since a date object like created_at without iterating over n.days.ago?

Upvotes: 0

Views: 1187

Answers (3)

FDI
FDI

Reputation: 801

(Time.current.to_date - Record.created_at.to_date).to_i 

As the returned value from Record.created_at is a string, you need to convert it to date as shown above

Upvotes: 0

sbs
sbs

Reputation: 4152

Date.today.downto(Record.created_at).count

Upvotes: 0

ForgetfulFellow
ForgetfulFellow

Reputation: 2632

From "Count number of days between two dates" and assuming that created_at returns a Date object, it seems like you can use:

(Time.current.to_date - Record.created_at).to_i 

to get the difference in days

Upvotes: 1

Related Questions