newbie question on how to subtract two dates.

Discussion in 'Ruby' started by Ruby Ruby, Dec 26, 2003.

  1. Ruby Ruby

    Ruby Ruby Guest

    --0-2040115834-1072473489=:21116
    Content-Type: text/plain; charset=us-ascii


    I have a file with a file name of the form filename.yymmdd, where yy=two digits year, mm=two digits month and dd=two digits day. For example, perf.030906 indicates 03=year 2003, 09=September and 06=the sixth day of the month (September 06, 2003).

    This file contains performance data collected during the day and each system contains one file per day. We have few dozen systems collecting this data.

    Every days, around 1:00 AM I have to take the file for the previous day and copy it to a central server where the new file will have the name: hostname.perf.yymmdd. The question I have is how do I subtract two dates. For example, if:

    today=010104

    yesterday=123103

    Is there a ruby method that will allow me to perform something like: today - x and get yesterday?

    Thank you


    ---------------------------------
    Do you Yahoo!?
    Protect your identity with Yahoo! Mail AddressGuard
    --0-2040115834-1072473489=:21116--
    Ruby Ruby, Dec 26, 2003
    #1
    1. Advertising

  2. Ruby Ruby

    Tim Hunter Guest

    Tim Hunter, Dec 26, 2003
    #2
    1. Advertising

  3. Ruby Ruby

    Jamis Buck Guest

    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


    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"'
    Jamis Buck, Dec 26, 2003
    #3
  4. On Saturday, December 27, 2003, 9:16:38 AM, Jamis wrote:

    > 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.)



    Thanks for the tip. I'll update the docs soon.

    Cheers,
    Gavin
    Gavin Sinclair, Dec 26, 2003
    #4
  5. Ruby Ruby wrote:
    > I have a file with a file name of the form filename.yymmdd, where yy=two digits year, mm=two digits month and dd=two digits day. For example, perf.030906 indicates 03=year 2003, 09=September and 06=the sixth day of the month (September 06, 2003).
    >
    > This file contains performance data collected during the day and each system contains one file per day. We have few dozen systems collecting this data.
    >
    > Every days, around 1:00 AM I have to take the file for the previous day and copy it to a central server where the new file will have the name: hostname.perf.yymmdd. The question I have is how do I subtract two dates. For example, if:
    >
    > today=010104
    >
    > yesterday=123103
    >
    > Is there a ruby method that will allow me to perform something like: today - x and get yesterday?
    >
    > Thank you
    >
    >
    > ---------------------------------
    > Do you Yahoo!?
    > Protect your identity with Yahoo! Mail AddressGuard

    You could use something like this:

    require 'date'
    puts today=Date.today #=>2003-12-27
    puts tomorrow=Date.new(2003,12,28) #=>2003-12-28
    puts today-1 #=>2003-12-26
    puts today+30 #=>2004-01-26

    cheers
    Adartse
    Osuka Adartse, Dec 27, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    859
    Rob Schieber
    Sep 30, 2005
  2. Manoj Nair
    Replies:
    4
    Views:
    21,333
    Manoj Nair
    Sep 4, 2003
  3. Lutek

    How to subtract dates ?

    Lutek, Jan 4, 2005, in forum: Java
    Replies:
    6
    Views:
    52,995
    pac0rro
    Apr 3, 2009
  4. barronmo

    subtract dates with time module

    barronmo, Mar 26, 2008, in forum: Python
    Replies:
    3
    Views:
    1,044
    barronmo
    Apr 2, 2008
  5. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    192
    Mark Schupp
    Aug 9, 2004
Loading...

Share This Page