Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

in correct date

Status
Not open for further replies.

LAdProg2005

Programmer
Feb 20, 2006
56
US
I have an issue with date difference. I am trying to find out difference in months by given date. But delta_ymd doesn't output proper difference..it individually subtracts year or month or day instead of doing date difference. may be i am missing something...
Please help. Thanks,

$dateM = ParseDate("12/17/2009");
$dateT = ParseDate("01/17/2010");

$y1 = &UnixDate($dateM,"%Y");
$m1 = &UnixDate($dateM,"%m");
$d1 = &UnixDate($dateM,"%d");
($y2, $m2, $d2) = &UnixDate($dateT,"%Y","%m", "%d");

($Dy , $Dm, $Dd) =
Delta_YMD($y1,$m1,$d1,$y2,$m2,$d2);
print $Dm;
 
Print out all your variables right before putting them in the Delta function so you can double-check right then that they're formatted correctly. Date::Calc wants its inputs formatted just right; refer to the docs to see what it's looking for.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
I printed the vars that are passed into
Delta_YMD($y1,$m1,$d1,$y2,$m2,$d2) are
: 2009,12,17,2010,01,17

between these two dates the difference should be of one month...it gives me difference of (1 -11 0) yr month day
:(

 
I figured he was using the Date::Calc module since it exports a Delta_YMD function.

Here's a quick test I did using the vars your script prints:

Code:
[kirsle@damocles ~]$ perl
use Date::Calc "Delta_YMD";
my ($y,$m,$d) = Delta_YMD(2009,12,17,2010,01,17);
print "y=$y; m=$m; d=$d\n";
__END__
y=1; m=-11; d=0

Do you get different results than this?

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
CPAN said:
($Dy,$Dm,$Dd) = Delta_YMD($year1,$month1,$day1, $year2,$month2,$day2);

This function returns the vector

( $year2 - $year1, $month2 - $month1, $day2 - $day1 )
So if you had RTFM'd, you'd see it was working as designed (NB. I never said it was either sensible or intuitive [smile]).

So it seems to be left as an exercise for the user to determine that 1 year - 11 months is in fact 1 month, which is kind of what you were expecting, just not in that format...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Take a look in the docs for Date::Calc for Normalize_Delta_YMD, it should help you out. For some more information, you might want to look at these two threads: thread219-1555637 and thread219-1524787
 
That helped a lot. Thank you all for helpful response. I appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top