473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7134
KevinADC
4,059 Expert 2GB
What are the date formats you will be working with?
Jul 18 '07 #2
numberwhun
3,509 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,509 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

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

Similar topics

2
by: Tiernan | last post by:
Hi all I'm looking for a way to find the number of weekdays between 2 dates In my form I have three fields for a begin date (dd)(mm)(yyyy) and three for the end date (dd)(mm)(yyyy) Now these...
5
by: SimonC | last post by:
Help needed for a Javascript beginner. As above in the subject... i need a javascript to run this, but not in the form of a web-page. I want to calculate it between 2 fields in a database that...
3
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the...
6
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
10
by: Brian Henry | last post by:
How would i take two dates startdate and enddate and subtract startdate from enddate to figure the number of days between the two? thanks
10
by: Scott Kilbourn | last post by:
Hi, Does anyone know how to accurately calculate the number of days that have elapsed since 01/01/0000? I'd appreciate any help anyone could give me. Thanks
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
6
by: Scott | last post by:
I want to work out the difference (number of days) between 2 dates selected using the date picker in vb.net and displaying this in textbox1 (which will be un-editable to the user) An MVP game me...
9
by: clintonb | last post by:
I'm looking for a way to calculate the number of days between two dates using standard C++ functions. Would it be as simple as just using the difftime() function and then dividing that result by...
1
by: bharathreddy | last post by:
This article will explain how we can get the count of weekdays in between two dates. This will be usefull if we want to count the number of working days between two dates. Example: -------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.