Posted By

gdonald on 09/27/06


Tagged

days between dates


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Firanide
webstic


Rails days between dates


 / Published in: Ruby
 

  1. def days_between(date1, date2)
  2. yyyy1,mm1,dd1 = $3, $1, $2 if date1 =~ /(\d+)\/(\d+)\/(\d+)/
  3. date1_ts = Time.mktime(yyyy1,mm1,dd1).tv_sec
  4. yyyy2,mm2,dd2 = $3, $1, $2 if date2 =~ /(\d+)\/(\d+)\/(\d+)/
  5. date2_ts = Time.mktime(yyyy2,mm2,dd2).tv_sec
  6. (date2_ts - date1_ts)/60/60/24
  7. end

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: angryamoeba on November 2, 2006

Or you can use normal ruby:

today = Date.today tomorrow = Date.today+1

tomorrow-today => 1

You need to login to post a comment.