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>)
6 7134
What are the date formats you will be working with?
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
-
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;
-
my $count = 0;
-
while($count == 0)
-
{
-
print "Enter from date\n";
-
chomp(my $fro_date = <STDIN>);
-
( $fro_day , $fro_mon , $fro_year ) = split /[\/\s\-\!\@\#\$\%\^\&\*\~\.\,]/, $fro_date;
-
if(($fro_day > 31)||($fro_mon > 12))
-
{
-
print "U entered day > 31 or mon > 12\n";
-
$count = 0;
-
}
-
else
-
{
-
if(((($fro_year % 4) == 0)&&($fro_day > 29))&&($fro_mon == 2))
-
{
-
print "U entered day > 29 in leapyear in february\n";
-
$count = 0;
-
}
-
elsif((($fro_day > 28)&&($fro_mon == 2))&&(($fro_year % 4)!= 0))
-
{
-
print "U entered day > 28 in normal year in february\n";
-
$count = 0;
-
}elsif((($fro_mon == 4)||($fro_mon == 6)||($fro_mon == 9)||($fro_mon == 11))&&($fro_day > 30))
-
{
-
print "U entered wrong day for the month $fro_mon \n";
-
$count = 0;
-
}
-
else
-
{
-
$count = 1;
-
}
-
}
-
}
-
$count = 0;
-
while($count == 0)
-
{
-
print "Enter to date\n";
-
chomp(my $to_date = <STDIN>);
-
( $to_day , $to_mon , $to_year ) = split /[\/\s\-\!\@\#\$\%\^\&\*\~\.\,]/, $to_date;
-
if(($to_day > 31)||($to_mon > 12))
-
{
-
print "U entered day > 31 or mon > 12\n";
-
$count = 0;
-
}
-
else
-
{
-
if(((($to_year % 4) == 0)&&($to_day > 29))&&($to_mon == 2))
-
{
-
print "U entered day > 29 in leapyear in february\n";
-
$count = 0;
-
}
-
elsif((($to_day > 28)&&($to_mon == 2))&&(($to_year % 4)!= 0))
-
{
-
print "U entered day > 28 in normal year in february\n";
-
$count = 0;
-
}
-
elsif((($to_mon == 4)||($to_mon == 6)||($to_mon == 9)||($to_mon == 11))&&($to_day > 30))
-
{
-
print "U entered wrong day for the month $fro_mon \n";
-
$count = 0;
-
}
-
else
-
{
-
$count = 1;
-
}
-
}
-
}
-
my $diff = $to_year - $fro_year;
-
my $temp_year = $fro_year;
-
if(($fro_mon == $to_mon)&&($fro_year == $to_year))
-
{
-
if($fro_year < 100)
-
{
-
$fro_year = $fro_year + 2000;
-
}
-
#print "$fro_year = 1\n";
-
if($to_year < 100)
-
{
-
$to_year = $to_year + 2000;
-
}
-
$mon = 0;
-
#print "$to_year = 1\n";
-
if($fro_mon == $to_mon)
-
{
-
if(($fro_mon == 1)||($fro_mon == 3)||($fro_mon == 5)||($fro_mon == 7)||($fro_mon == 8)||($fro_mon == 10)||($fro_mon == 12))
-
{
-
$mon = 31;
-
}elsif((($fro_year % 4)== 0)&&($fro_mon == 2))
-
{
-
$mon = 29;
-
}elsif($fro_mon == 2)
-
{
-
$mon = 28;
-
}
-
if(($fro_mon == 4)||($fro_mon == 6)||($fro_mon == 9)||($fro_mon == 11))
-
{
-
$mon = 30;
-
}
-
}
-
if($fro_day > $to_day)
-
{
-
if(($fro_day == $mon)&&($to_day == 1))
-
{
-
print "U r checking the total no.of days for the month $fro_mon are $mon\n";
-
}else
-
{
-
print "The total no.of days are",-($fro_day - $to_day),"\n";
-
}
-
}elsif($to_day > $fro_day)
-
{
-
if(($fro_day == 1)&&($to_day == $mon))
-
{
-
print "U r checking the total no.of days for the month $fro_mon are $mon\n";
-
}else
-
{
-
print "The total no.of days are",-($fro_day - $to_day),"\n";
-
}
-
}
-
else
-
{
-
print "The total no.of days are 0 \n";
-
}
-
}else
-
{
-
if($fro_year < 100)
-
{
-
$fro_year = $fro_year + 2000;
-
}
-
#print "$fro_year = 2\n";
-
if($to_year < 100)
-
{
-
$to_year = $to_year + 2000;
-
}
-
#print "$to_year = 2\n";
-
my $diff = $to_year - $fro_year;
-
if($fro_year)
-
{
-
my $i = $fro_mon;
-
$fro_days = 0;
-
$mon = 0;
-
while($i <= 12)
-
{
-
if(($i == 1)||($i == 3)||($i == 5)||($i == 7)||($i == 8)||($i == 10)||($i == 12))
-
{
-
$mon = 31;
-
if($i == $fro_mon)
-
{
-
$mon = $mon - $fro_day;
-
}
-
}elsif((($fro_year % 4)== 0)&&($i == 2))
-
{
-
$mon = 29;
-
if($i == $fro_mon)
-
{
-
$mon = $mon - $fro_day;
-
}
-
}elsif($i == 2)
-
{
-
$mon = 28;
-
if($i == $fro_mon)
-
{
-
$mon = $mon - $fro_day;
-
}
-
}
-
if(($i == 4)||($i == 6)||($i == 9)||($i == 11))
-
{
-
$mon = 30;
-
if($i == $fro_mon)
-
{
-
$mon = $mon - $fro_day;
-
}
-
}
-
$fro_days = $fro_days + $mon;
-
$i++;
-
}
-
}
-
#print "Remaining days from 01/$fro_mon/$fro_year to 31/12/$fro_year = $fro_days\n";
-
if($to_year)
-
{
-
my $i = 1;
-
$to_days = 0;
-
$mon = 0;
-
while($i < $to_mon)
-
{
-
if(($i == 1)||($i == 3)||($i == 5)||($i == 7)||($i == 8)||($i == 10)||($i == 12))
-
{
-
$mon = 31;
-
if($i == $to_mon)
-
{
-
$mon = $mon - $to_day;
-
}
-
}elsif((($to_year % 4)== 0)&&($i == 2))
-
{
-
$mon = 29;
-
if($i == $to_mon)
-
{
-
$mon = $mon - $to_day;
-
}
-
}elsif($i == 2)
-
{
-
$mon = 28;
-
if($i == $to_mon)
-
{
-
$mon = $mon - $to_day;
-
}
-
}
-
if(($i == 4)||($i == 6)||($i == 9)||($i == 11))
-
{
-
$mon = 30;
-
if($i == $to_mon)
-
{
-
$mon = $mon - $to_day;
-
}
-
}
-
$to_days = $to_days + $mon;
-
$i++;
-
}
-
$to_days = $to_days + $to_day;
-
}
-
#print "Remaining days from 01/01/$to_year to $to_day/$to_mon/$to_year = $to_days\n";
-
$year = 0;
-
if($diff == 1)
-
{
-
print "The total no.of days are", ($fro_days+$to_days),"\n";
-
}
-
elsif($diff > 1)
-
{
-
#print "fro_year = $fro_year to_year = $to_year diff = $diff\n";
-
$temp_year = $fro_year + 1;
-
$temp_total = 0;
-
while($temp_year < $to_year)
-
{
-
if(($temp_year % 4) == 0)
-
{
-
$year = 366;
-
}
-
else
-
{
-
$year = 365;
-
}
-
$temp_total = $temp_total + $year;
-
$temp_year++;
-
}
-
print "The total no.of days are ", ($temp_total+$fro_days+$to_days),"\n";
-
}
-
elsif($fro_year == $to_year)
-
{
-
$i= $fro_mon;
-
$mon = 0;
-
$temp_total = 0;
-
while($i < $to_mon)
-
{
-
if(($i == 1)||($i == 3)||($i == 5)||($i == 7)||($i == 8)||($i == 10)||($i == 12))
-
{
-
$mon = 31;
-
if($i == $fro_mon)
-
{
-
$mon = $mon - $fro_day;
-
}
-
}elsif((($fro_year % 4)== 0)&&($i == 2))
-
{
-
$mon = 29;
-
if($i == $fro_mon)
-
{
-
$mon = $mon - $fro_day;
-
}
-
}elsif($i == 2)
-
{
-
$mon = 28;
-
if($i == $fro_mon)
-
{
-
$mon = $mon - $fro_day;
-
}
-
}
-
if(($i == 4)||($i == 6)||($i == 9)||($i == 11))
-
{
-
$mon = 30;
-
if($i == $fro_mon)
-
{
-
$mon = $mon - $fro_day;
-
}
-
}
-
$temp_total = $temp_total + $mon;
-
#print "The total is $temp_total\n";
-
$i++;
-
}
-
$temp_total = $temp_total + $to_day;
-
print "The total no.of days are $temp_total \n";
-
}
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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
|
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
|
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...
|
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...
|
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...
|
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:
-------------...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |