From: Jamis Buck Date: 2003-12-27T07:16:38+09:00 Subject: Re: newbie question on how to subtract two dates. Tim Hunter wrote: >On Sat, 27 Dec 2003 06:18:33 +0900, Ruby Ruby wrote: > > > >>Is there a ruby method that will allow me to perform something like: >>today - x and get yesterday? >> >> >> >Check out the Date class. >http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/files/date_rb.html > >. > > > You just need to subtract (or add) the number of seconds you want from a Date to get another date: in other words, to find the date 24 hours prior to the current date: yesterday = Time.now - 24 * 60 * 60 (Incidentally, I looked at the Date class documentation... I already knew how to do Date arithmetic in Ruby and knew what I was looking for, and it still took me a while to find where in the documentation it talks about how to subtract some value x from a date to get another date. Since this is a fairly common operation on dates, could this be given as an example somewhere near the front of the docs? I know I would never have found it there if I didn't already know what it was I wanted to do, and how to do it.) -- Jamis Buck jgb3@email.byu.edu ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'