469,285 Members | 2,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,285 developers. It's quick & easy.

Number of days between two dates

Hello,

I am just new to Perl programming. I have installed Perl5.6 in my system. I would like to find number of days between two dates. But I don't want to use any modules. Could someone help me to solve this problem.

Thanks,
Dilip Kumar M.R.
(<removed>)
Jul 18 '07 #1
6 6869
KevinADC
4,059 Expert 2GB
What are the date formats you will be working with?
Jul 18 '07 #2
numberwhun
3,503 Expert Mod 2GB
Hello,

I am just new to Perl programming.
That's fine, we all have to start somewhere.

I have installed Perl5.6 in my system.
Another good thing. :-)

I would like to find number of days between two dates. But I don't want to use any modules. Could someone help me to solve this problem.
)
Can you provide the code that you have tried thus far? We could certainly provide assistance, but we aren't into writing your scripts for you. This is a help forum, not a service. :-) If you post code, we can help debug and guide you in the right direction.

Regards,

Jeff
Jul 18 '07 #3
archulu
34
Expand|Select|Wrap|Line Numbers
  1. my $fro_day,$fro_mon,$fro_year,$to_day,$to_mon,$to_year,$months,$days,$tot_days,$mon,$year,$fro_days,$to_days,$temp_total;
  2.     my $count = 0;
  3.     while($count == 0)
  4.     {
  5.         print "Enter from date\n";
  6.         chomp(my $fro_date = <STDIN>);
  7.         ( $fro_day , $fro_mon , $fro_year ) = split  /[\/\s\-\!\@\#\$\%\^\&\*\~\.\,]/, $fro_date;
  8.         if(($fro_day > 31)||($fro_mon > 12))
  9.         {
  10.             print "U entered day > 31 or mon > 12\n";
  11.             $count = 0;
  12.         }
  13.         else
  14.         {
  15.             if(((($fro_year % 4) == 0)&&($fro_day > 29))&&($fro_mon == 2))
  16.             {
  17.                 print "U entered day > 29 in leapyear in february\n";
  18.                 $count = 0;
  19.             }
  20.             elsif((($fro_day > 28)&&($fro_mon == 2))&&(($fro_year % 4)!= 0))
  21.             {
  22.                 print "U entered day > 28 in normal year in february\n";
  23.                 $count = 0;
  24.             }elsif((($fro_mon == 4)||($fro_mon == 6)||($fro_mon == 9)||($fro_mon == 11))&&($fro_day > 30))
  25.             {
  26.                 print "U entered wrong day for the month $fro_mon \n";
  27.                 $count = 0;
  28.             }
  29.             else
  30.             {
  31.                 $count = 1;
  32.             }
  33.         }
  34.     }
  35.         $count = 0;
  36.         while($count == 0)
  37.         {
  38.                 print "Enter to date\n";
  39.                 chomp(my $to_date = <STDIN>);
  40.                 ( $to_day , $to_mon , $to_year ) = split  /[\/\s\-\!\@\#\$\%\^\&\*\~\.\,]/, $to_date;
  41.                 if(($to_day > 31)||($to_mon > 12))
  42.                 {
  43.                         print "U entered day > 31 or mon > 12\n";
  44.                         $count = 0;
  45.                 }
  46.                 else
  47.                 {
  48.                         if(((($to_year % 4) == 0)&&($to_day > 29))&&($to_mon == 2))
  49.                         {
  50.                                 print "U entered day > 29 in leapyear in february\n";
  51.                                 $count = 0;
  52.                         }
  53.                         elsif((($to_day > 28)&&($to_mon == 2))&&(($to_year % 4)!= 0))
  54.                         {
  55.                                 print "U entered day > 28 in normal year in february\n";
  56.                                 $count = 0;
  57.                         }
  58.             elsif((($to_mon == 4)||($to_mon == 6)||($to_mon == 9)||($to_mon == 11))&&($to_day > 30))
  59.                         {
  60.                                 print "U entered wrong day for the month $fro_mon \n";
  61.                                 $count = 0;
  62.                         }
  63.             else
  64.                         {
  65.                                 $count = 1;
  66.                         }
  67.                 }
  68.         }
  69.     my $diff = $to_year - $fro_year;
  70.     my $temp_year = $fro_year;
  71.     if(($fro_mon == $to_mon)&&($fro_year == $to_year))
  72.     {
  73.                 if($fro_year < 100)
  74.                 {
  75.                         $fro_year = $fro_year + 2000;
  76.                 }
  77.         #print "$fro_year = 1\n";
  78.                 if($to_year < 100)
  79.                 {
  80.                         $to_year = $to_year + 2000;
  81.                 }
  82.         $mon = 0;
  83.         #print "$to_year = 1\n";
  84.                 if($fro_mon == $to_mon)
  85.                 {
  86.                     if(($fro_mon == 1)||($fro_mon == 3)||($fro_mon == 5)||($fro_mon == 7)||($fro_mon == 8)||($fro_mon == 10)||($fro_mon == 12))
  87.                         {
  88.                                 $mon = 31;
  89.                         }elsif((($fro_year % 4)== 0)&&($fro_mon == 2))
  90.                         {
  91.                                 $mon = 29;
  92.                         }elsif($fro_mon == 2)
  93.                         {
  94.                                 $mon = 28;
  95.                         }
  96.                         if(($fro_mon == 4)||($fro_mon == 6)||($fro_mon == 9)||($fro_mon == 11))
  97.                         {
  98.                                 $mon = 30;
  99.                         }
  100.                 }
  101.                 if($fro_day > $to_day)
  102.         {
  103.             if(($fro_day == $mon)&&($to_day == 1))
  104.             {
  105.                 print "U r checking the total no.of days for the month $fro_mon are $mon\n";
  106.             }else
  107.             {
  108.                 print "The total no.of days are",-($fro_day - $to_day),"\n";
  109.             }
  110.         }elsif($to_day > $fro_day)
  111.         {
  112.                         if(($fro_day == 1)&&($to_day == $mon))
  113.                         {
  114.                                 print "U r checking the total no.of days for the month $fro_mon are $mon\n";
  115.                         }else
  116.                         {
  117.                                 print "The total no.of days are",-($fro_day - $to_day),"\n";
  118.                         }
  119.         }
  120.         else
  121.         {
  122.             print "The total no.of days are 0 \n";
  123.         }
  124.     }else
  125.     {
  126.         if($fro_year < 100)
  127.         {
  128.             $fro_year = $fro_year + 2000;
  129.         }
  130.         #print "$fro_year = 2\n";
  131.         if($to_year < 100)
  132.         {
  133.             $to_year = $to_year + 2000;
  134.         }
  135.         #print "$to_year = 2\n";
  136.             my $diff = $to_year - $fro_year;
  137.         if($fro_year)
  138.         {
  139.             my $i = $fro_mon;
  140.             $fro_days = 0;
  141.             $mon = 0;
  142.             while($i <= 12)
  143.             {
  144.                 if(($i == 1)||($i == 3)||($i == 5)||($i == 7)||($i == 8)||($i == 10)||($i == 12))
  145.                 {
  146.                     $mon = 31;
  147.                     if($i == $fro_mon)
  148.                     {
  149.                         $mon = $mon - $fro_day;
  150.                     }
  151.                 }elsif((($fro_year % 4)== 0)&&($i == 2))
  152.                 {
  153.                     $mon = 29;
  154.                                     if($i == $fro_mon)
  155.                                     {
  156.                                             $mon = $mon - $fro_day;
  157.                                     }
  158.                     }elsif($i == 2)
  159.                     {
  160.                         $mon = 28;
  161.                                         if($i == $fro_mon)
  162.                                         {
  163.                                                 $mon = $mon - $fro_day;
  164.                                         }
  165.                     }
  166.                     if(($i == 4)||($i == 6)||($i == 9)||($i == 11))
  167.                     {
  168.                         $mon = 30;
  169.                                         if($i == $fro_mon)
  170.                                         {
  171.                                                 $mon = $mon - $fro_day;
  172.                                     }
  173.                 }
  174.                 $fro_days = $fro_days + $mon;
  175.                 $i++;
  176.             }
  177.         }
  178.         #print "Remaining days from 01/$fro_mon/$fro_year  to  31/12/$fro_year = $fro_days\n";
  179.             if($to_year)
  180.             {
  181.                     my $i = 1;
  182.                     $to_days = 0;
  183.                     $mon = 0;
  184.                     while($i < $to_mon)
  185.                     {
  186.                             if(($i == 1)||($i == 3)||($i == 5)||($i == 7)||($i == 8)||($i == 10)||($i == 12))
  187.                             {
  188.                                     $mon = 31;
  189.                                     if($i == $to_mon)
  190.                                     {
  191.                                             $mon = $mon - $to_day;
  192.                                     }
  193.                             }elsif((($to_year % 4)== 0)&&($i == 2))
  194.                             {
  195.                                     $mon = 29;
  196.                                     if($i == $to_mon)
  197.                                     {
  198.                                             $mon = $mon - $to_day;
  199.                                     }
  200.                             }elsif($i == 2)
  201.                             {
  202.                                     $mon = 28;
  203.                                     if($i == $to_mon)
  204.                                     {
  205.                                             $mon = $mon - $to_day;
  206.                                     }
  207.                             }
  208.                             if(($i == 4)||($i == 6)||($i == 9)||($i == 11))
  209.                             {
  210.                                     $mon = 30;
  211.                                     if($i == $to_mon)
  212.                                     {
  213.                                             $mon = $mon - $to_day;
  214.                                     }
  215.                             }
  216.                             $to_days = $to_days + $mon;
  217.                             $i++;
  218.                     }
  219.             $to_days = $to_days + $to_day;
  220.             }
  221.             #print "Remaining days from 01/01/$to_year  to  $to_day/$to_mon/$to_year = $to_days\n";
  222.         $year = 0;
  223.         if($diff == 1)
  224.         {
  225.             print "The total no.of days are", ($fro_days+$to_days),"\n";
  226.         }
  227.         elsif($diff > 1)
  228.         {
  229.             #print "fro_year = $fro_year to_year = $to_year diff = $diff\n";
  230.             $temp_year = $fro_year + 1;
  231.             $temp_total = 0;
  232.             while($temp_year < $to_year)
  233.             {
  234.                 if(($temp_year % 4) == 0)
  235.                 {
  236.                     $year = 366;
  237.                 }
  238.                 else
  239.                 {
  240.                     $year = 365;
  241.                 }
  242.                 $temp_total = $temp_total + $year;
  243.                 $temp_year++;
  244.             }
  245.             print "The total no.of days are ", ($temp_total+$fro_days+$to_days),"\n";
  246.         }
  247.         elsif($fro_year == $to_year)
  248.         {
  249.             $i= $fro_mon;
  250.             $mon = 0;
  251.             $temp_total = 0;
  252.                     while($i < $to_mon)
  253.                     {
  254.                             if(($i == 1)||($i == 3)||($i == 5)||($i == 7)||($i == 8)||($i == 10)||($i == 12))
  255.                             {
  256.                                     $mon = 31;
  257.                                     if($i == $fro_mon)
  258.                                     {
  259.                                             $mon = $mon - $fro_day;
  260.                                     }
  261.                             }elsif((($fro_year % 4)== 0)&&($i == 2))
  262.                             {
  263.                                     $mon = 29;
  264.                                     if($i == $fro_mon)
  265.                                     {
  266.                                             $mon = $mon - $fro_day;
  267.                                     }
  268.                             }elsif($i == 2)
  269.                             {
  270.                                     $mon = 28;
  271.                                     if($i == $fro_mon)
  272.                                     {
  273.                                             $mon = $mon - $fro_day;
  274.                                     }
  275.                             }
  276.                             if(($i == 4)||($i == 6)||($i == 9)||($i == 11))
  277.                             {
  278.                                     $mon = 30;
  279.                                     if($i == $fro_mon)
  280.                                     {
  281.                                             $mon = $mon - $fro_day;
  282.                                     }
  283.                             }
  284.                             $temp_total = $temp_total + $mon;
  285.                 #print "The total is $temp_total\n";
  286.                             $i++;
  287.             }
  288.             $temp_total = $temp_total + $to_day;
  289.             print "The total no.of days are $temp_total \n";
  290.         }
  291.     }
  292.  
Sep 26 '07 #5
eWish
971 Expert 512MB
That sure looks like a lot of work. I would loot at CPAN and try one of the date modules.

Date::Manip
Time::Object
Date::Calc
Sep 26 '07 #6
numberwhun
3,503 Expert Mod 2GB
That sure looks like a lot of work. I would loot at CPAN and try one of the date modules.

Date::Manip
Time::Object
Date::Calc
I have to agree with eWish, that is A LOT of work in coding when it could be done with much less lines.

Personally, you want to find the amount of time elapsed between two dates, then you could use the Time::Local module off of CPAN. It allows you to convert a date into seconds. You would convert the two dates to seconds, subtract the two to get the difference and then divide by 86400 (the number of seconds in one day).

Regard,

Jeff
Sep 26 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Tiernan | last post: by
3 posts views Thread by jerry.ranch | last post: by
10 posts views Thread by Scott Kilbourn | last post: by
29 posts views Thread by james | last post: by
6 posts views Thread by Scott | last post: by
9 posts views Thread by clintonb | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.