Ruby Arithmetic Operation for Date

I wanted to get the difference between two dates. My date are in format “DD/MM/YYYY”. I tried to get number of days between two date of same format. Finally I got an easy method to get number of day between these two date.

For this first of all I changed these date to date format using following method.

d1 = “12/2/2012”
d2 = “15/2/2012”
d1 = d1.to_date
d2 = d2.to_date
no_of_days = (d2-d1).to_i

Finally, I get number of days between two date. If there are any better methods for this operation, suggestions are welcome.

One thought on “Ruby Arithmetic Operation for Date

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s