Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Calculate date with days

by darrengan (Sexton)
on Aug 24, 2005 at 05:01 UTC ( [id://486096]=perlquestion: print w/replies, xml ) Need Help??

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

Hie friends, How do i use system date to calculate a date with a given number of days? e.g. 24/08/2005 add 3 days = 27/08/2005 and 24/08/2005 add 10 days = 03/09/2005 any idea? Cheers mate, Darren

Florist In Malaysia

Replies are listed 'Best First'.
Re: Calculate date with days
by chester (Hermit) on Aug 24, 2005 at 05:27 UTC
    I assume there's a typo in your post, and the second date result should be 03/09/2005?

    So far as installing modules, it's usually as simple as running perl -MCPAN -e 'install Date::Calc' if you use *nix, or using ppm in Windows. You can see here for more info: http://www.cpan.org/misc/cpan-faq.html

    This is another solution to your question, using Date::Calc:

    use strict; use warnings; use Date::Calc qw(Add_Delta_Days); my ($year, $month, $day); ($year, $month, $day) = Add_Delta_Days(2005,8,24,3); printf "%02d/%02d/%4d\n", $day, $month, $year; ($year, $month, $day) = Add_Delta_Days(2005,8,24,10); printf "%02d/%02d/%4d\n", $day, $month, $year;
      I almost always use an array instead of ($year, $month, $day) when working with Date::Calc -- faster to type and clearer to read (especially with the functions like Delta_Days that take two dates). (of course functionally it's exactly the same)
      use Date::Calc qw(Add_Delta_Days); my $date_str = "24/08/2005"; my @start = reverse split "/", $date_str; # convert to (YYYY, MM, DD +) array my @d = Add_Delta_Days(@start,3); printf "%02d/%02d/%4d\n", $d[2,1,0]; # print DD/MM/YYYY format @d = Add_Delta_Days(@start,10); printf "%02d/%02d/%4d\n", $d[2,1,0]; # print DD/MM/YYYY format
(dkubb) Re: (1) Calculate date with days
by dkubb (Deacon) on Aug 24, 2005 at 08:44 UTC
    Here's another approach using DateTime:
    use strict; use warnings; use DateTime; my $dt = DateTime->new(year => '2005', month => '08', day => '24'); $dt->add(days => 10); print $dt->dmy('/'), "\n";
    Dan Kubb, Perl Programmer
Re: Calculate date with days
by pg (Canon) on Aug 24, 2005 at 05:07 UTC
    use Data::Dumper; use strict; use warnings; my $offset = 3; print Dumper(localtime(time())); print "\n"; print Dumper(localtime(time() + 24 * 3600 * $offset));
      hie pg, i am totally new to perl. for the Data::Dumper, do i need to install this component or it is already included in the system? Cheers, Darren

        Data::Dumper is actually not needed, and it was just a handy way to display the result:

        use strict; use warnings; my $offset = 3; display(reverse((localtime(time()))[0 .. 5])); display(reverse((localtime(time() + 24 * 3600 * $offset))[0 .. 5])); sub display { my @t = @_; printf("%d-%02d-%02d-%02d:%02d:%02d\n", $t[0] + 1900, $t[1] + 1, @ +t[2 .. 5]); }
        Data::Dumper is a core perl Module i.e. should come with Perl. Check the link to see more information about it.

        A simple use case

        #!/usr/bin/perl use Data::Dumper; use strict; use warnings; my @list = qw (hi there); print Dumper(@list);

        Output

        $VAR1 = 'hi'; $VAR2 = 'there';
Re: Calculate date with days
by fauria (Deacon) on Aug 24, 2005 at 08:05 UTC
    #cdate.pl use strict; use Date::Manip qw (DateCalc ParseDate UnixDate); my $input = ParseDate shift or die 'I need a date'; my $date = DateCalc $input, "+3 days"; my @format = ('%B ', '%e, ', '%Y'); print UnixDate($date, @format)."\n";
    Example:
    perl cdate.pl 20050824
    perl cdate.pl 08/24/2005
    perl cdate.pl `date`
Re: Calculate date with days
by darrengan (Sexton) on Aug 25, 2005 at 02:21 UTC
    Hie fellow friends... Thanks for the feedback and tips. I found that the script provided by "pg" eventually solved my problem. It is straight forward. my $offset = 3; display(reverse((localtime(time()))0 .. 5)); display(reverse((localtime(time() + 24 * 3600 * $offset))0 .. 5)); sub display { my @t = @_; printf("%d-%02d-%02d-%02d:%02d:%02d\n", $t[0] + 1900, $t1 + 1, @ +t2 .. 5); } Thanks so much
Re: Calculate date with days
by sunadmn (Curate) on Aug 24, 2005 at 14:30 UTC
    I would suggest you take a look at Time::HiRes Thi s should take care of all the hard work for you.
    SUNADMN
    USE PERL

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://486096]
Approved by sk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-16 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found