We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

gdonald on 09/27/06


Tagged

days between dates


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

Firanide


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.