trueCamelType trueCamelType - 1 year ago 70
Ruby Question

How can I subtract formatted dates in Ruby, to return a day difference?

This is my first time using Ruby, and I've been given two dates. These two dates are always within 7 days of each other, and I need to get the difference between them.

My issue is, that one of these dates comes from a database, and is formatted like this


My other date is just
, but I can format it like the other using"%Y-%m-%d")

I don't necessarily think I need to format the second date, but I was hoping if I got them both in the same format, I could just subtract them....which does not appear to be the case.

Any ideas how I could have this just return an integer for how many days are between the two dates, if one date is a pre-formatted date from a db?

Answer Source

(date1.to_date - date2.to_date).to_i should give you the difference in days.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download