473,573 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Number of days between two dates

1 New Member
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 7137
KevinADC
4,059 Recognized Expert Specialist
What are the date formats you will be working with?
Jul 18 '07 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
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
miller
1,089 Recognized Expert Top Contributor
perlfaq4 How can I compare two dates and find the difference?

- Miller
Jul 18 '07 #4
archulu
34 New Member
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 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
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
8049
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 values will be stored into a database and at the same time there will be an e-mail generated and send to me that contains the starting date en the end...
5
6722
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 i have extracted into a report writer. Look forward to hearing.. Cheers... SimonC
3
29026
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 week . I've used the weekday() function to convert dates to numberic days of the week (1-7) I've used cdbl (date) to convert a date to a serial...
6
27960
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 to the right of these 2 fields to show the
10
12977
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
5374
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
9078
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 example is: 36,525 represents 01/01/1900. The starting point date is considered to be : 00/00/0000. I have looked thru Help and used Google and...
6
17451
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 the following code, which looks acceptable to me in practice, but appears to always through up 0 in textbox1??? Am I doing anything blatantly...
9
21377
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 the number of seconds in a day? - Clint
1
7201
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: ------------- USE GO --This function is used to count the number of weekdays between -- two dates. create function udf_Weekdays(@Weekday,@BeginDate...
0
7784
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8032
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8205
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7796
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8074
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6424
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5601
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.