0

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
wenincode
  • 376
  • 5
  • 10
  • 20
  • Does `created_at` return a Date Object? – ForgetfulFellow Jan 29 '16 at 00:08
  • Welcome to Stack Overflow. Please read "[ask]" and "[mcve]". As is it looks like you want us to write code for you, which is not what Stack Overflow is for. Please include the minimal example of the code you've written and we'll help you fix it. If you haven't written code, why not? Where have you searched and what about those references didn't help you? http://meta.stackoverflow.com/q/261592/128421 and http://tinyurl.com/stack-hints are also good reads. – the Tin Man Jan 29 '16 at 00:17

3 Answers3

1

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

Community
  • 1
  • 1
ForgetfulFellow
  • 2,477
  • 2
  • 22
  • 33
0
Date.today.downto(Record.created_at).count
sbs
  • 4,102
  • 5
  • 40
  • 54
0
(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

FDI
  • 771
  • 1
  • 9
  • 25