Perl_Student has asked for the wisdom of the Perl Monks concerning the following question:

How do you add number of days to a user provided date which is in the format of mm/dd/yy?
  • Comment on How do you add dates to a certain user provided date?

Replies are listed 'Best First'.
Re: How do you add dates to a certain user provided date?
by arden (Curate) on Mar 09, 2004 at 17:04 UTC
    I'd use Date::Calc or one of the other modules on Date from CPAN, but like Old_Gray_Bear said, your professor isn't likely to accept that solution. Try something yourself and if it doesn't work, come back and ask for help with your homework, but don't ask us to do it for you. . .

    - - arden.

    Update:, if you are or aren't a student, please read How (Not) To Ask A Question.

      Not a student and neither a homework. Used this nick to indicate I am learning perl
Re: How do you add dates to a certain user provided date?
by Old_Gray_Bear (Bishop) on Mar 09, 2004 at 17:00 UTC
    I would convert the date into an epoch-value and add. (I suspect that your Instructor will not let you use this answer, however.)

    ----
    I Go Back to Sleep, Now.

    OGB

Re: How do you add dates to a certain user provided date?
by jdtoronto (Prior) on Mar 09, 2004 at 20:00 UTC
    #!/usr/local/bin/perl use Time::Piece; use Time::Seconds; use strict; my $time = Time::Piece->strptime( '2004-03-09 15:30:00', '%Y-%m-%d % +H:%M:%S' ); my $new_time = $time + (ONE_DAY * 3) print "$new_time\n";
    Is another way to do it.

    jdtoronto

Re: How do you add dates to a certain user provided date?
by Bklyn (Initiate) on Mar 09, 2004 at 17:16 UTC
    Check out the Date::Calc module. It has all sorts of magic for date operations and arithmetic. You'll need to parse the mm/dd/yy date yourself before calling the Date::Calc routines though as they take the date broken down into year, month, day triples.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How do you add dates to a certain user provided date?
by AcidHawk (Vicar) on Mar 10, 2004 at 07:31 UTC
    I like the idea of converting to epoc time. And you can do this with core the Time::Local module.

    You still face a challenge in that the year is not in ccyy format. So you need to work out is it 1904 or 2004 that you are dealing with.

    Let's assume that you adddress this year problem and that based on your comment that this is not homework, look at this to convert the date from mm/dd/ccyy to epoc.

    $date = "03/10/2004"; my @d = split/\\/, $date; # Second, Min , Hour , Day , Month, Year $epoc = timelocal(0, 0, 0, $d[1], $d[0], $d[2]);
    You could then take $epoc and add ($num_of_days_to_add * 86400) which is the number of seconds in a day. All that's left to do is convert this new epoc number to a valid date.

    ($ss, $mm, $hh, $d, $mon, $yr) = localtime($epoc);
    You can now build how you want to diaplsy your new date. Something like..
    printf("NewDate: %02d/%02d/%04d\n", $mon+1, $d, $yr+1900);
    HTH
    -----
    Of all the things I've lost in my life, its my mind I miss the most.