scripter87 has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys I am very new to perl and programming in general. I have a question about getting the amount of days between two dates. I do not want to use any modules- (just A LOT of code). Here is my code so far any advice for me.? I cant get my head around trying to get the right number of days in different years- as you will see in the code
#!/usr/bin/perl use strict; use warnings; print "please enter day of birth: "; my $d = <STDIN>; print "please enter month of birth: "; my $m = <STDIN>; print "please enter year of birth: "; my $y = <STDIN>; chomp($d, $m,$y);#strip newlines print "please enter current day: "; my $cd = <STDIN>; print "please enter current month: "; my $cm = <STDIN>; print "please enter current year: "; my $cy = <STDIN>; chomp($cd, $cm,$cy); my$days=0; if ($y>$cy){ warn "Birth Year Cannot be after the current year."; } elsif ($y==$cy){ $days= $days + NumberOfDaysBetween($d,$cd ,$m,$cm ); print "$days"; } else { my$years= ($cy-$y) * 365; $days= $days + NumberOfDaysBetween($d,31,$m,12) + NumberOfDaysBetween( +1,$cd,1,$cm) + $years; print "war war$days"; } sub NumberOfDaysBetween { my$daysBetween; my$startDay=shift; my$endDay=shift; my$sMon=shift; my$eMon=shift; if ($y <= $cy){ if ($sMon > $eMon || $sMon == $eMon&& $startDay > $endDay){ print "invalid input"; } elsif ($sMon == $eMon && $startDay<= $endDay){ $daysBetween = $endDay- $startDay; } else { $daysBetween =((&NumberOfDaysInMonth($sMon))-($startDay-1)) + +$endDay + &cumulativeDaysInMonths($sMon,$eMon); } } return $daysBetween; } sub NumberOfDaysInMonth { my $mon=shift; my$numOfDaysInMonth; if ($mon == 2) { $numOfDaysInMonth = 28; } elsif ($mon == 9|| $mon == 4|| $mon == 6|| $mon == 11) { $numOfDaysInMonth = 30; } else { $numOfDaysInMonth = 31; } return $numOfDaysInMonth; } sub cumulativeDaysInMonths { my $sMonth=shift; my $eMonth=shift; my$daysRange=0; $eMonth--; while ($eMonth>$sMonth){ $daysRange=$daysRange + NumberOfDaysInMonth($eMonth); $eMonth-- } return$daysRange; }
|
|---|