Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

(zdog) Re: Weird Date::Calc behavior; Bug?

by zdog (Priest)
on Apr 19, 2001 at 08:51 UTC ( #73759=note: print w/replies, xml ) Need Help??

in reply to Weird Date::Calc behavior; Bug?

You cannot assign a scalar to an array that way. You should instead split the scalar up like so:

#!/usr/bin/perl -w use strict; use Date::Calc qw ( Delta_Days Add_Delta_Days ); my (@start, @stop); @start = split (/-/, $ARGV[0]); print "@start\n"; @stop = split (/-/, $ARGV[1]); print "@stop\n"; for ( my $i = 0; $i <= Delta_Days(@start, @stop); $i++ ) { my @date = Add_Delta_Days (@start, $i); printf ("%4d-%02d-%02d\n", @date); }
Update: You can also do something like:

$from =~ s/-/,/g; $to =~ s/-/,/g; @start = eval ($from); @stop = eval ($to);

Zenon Zabinski | zdog |

Replies are listed 'Best First'.
Re^2: Weird Date::Calc behavior; Bug?
by Anonymous Monk on Mar 06, 2006 at 18:02 UTC
    I have a different problem - but the title of the thread and the code are similar...

    Here's the code...

    #!/usr/bin/perl use Date::Calc qw( Delta_Days Add_Delta_Days); use strict; my ($year1,$month1,$day1) = (2006,3,6); my ($year2,$month2,$day2) = (1997,5,8); print"Todays date is $day1\/$month1\/$year1\n"; print"The date in the past is $day2\/$month2\/$year2\n"; # Get the difference in days between the two dates my $daysdiff = Delta_Days($year2,$month2,$day2,$year1,$month1,$day1); # Add the difference to 01/01/0001 to get the long date my ($diffyear,$diffmonth,$diffday) = Add_Delta_Days(1,1,1,$daysdiff); print"The difference between $day2\/$month2\/$year2 and $day1\/$month1 +\/$year1 is ...\n"; print"$diffyear Years, $diffmonth Months and $diffday Days\n";
    The output :
    $ ./ Todays date is 6/3/2006 The date in the past is 8/5/1997 The difference between 8/5/1997 and 6/3/2006 is ... 9 Years, 10 Months and 30 Days

    Now, using my fingers, I make the difference between the dates 8/5/1997 and 6/3/2006 ...
    8 Years, 9 months and 2 days.

    Why the different result from Date::Calc? Am I doing something wrong? It's not even close!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://73759]
[Discipulus]: problematic? i came back after just a week of course, I forgo teverything..

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-12-18 11:24 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (483 votes). Check out past polls.