Hi,
I am new to Perl Programming and have a query in perl.
In perl is there any system defined functions to find out the Differences in dates.
Eg :
Date 1 -> 26-Jan-2009
Date 2 -> 14-Jan-2009
So the difference between two dates is 12 days.
Is there a way to achieve this with any system defined functions In Perl ????
-Rajini
Please go to cpan.org and search for Perl modules that have "date"
in their names; you'll find several that will do what you ask.
*----------------------------------------------------------------------*
| Tim Maher, PhD (206) 781-UNIX http://www.consultix-inc.com |
| tim at ( TeachMePerl, TeachMeLinux, or TeachMeUnix ) dot Com |
*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
| > "Minimal Perl for UNIX People" has been an Amazon Best Seller! < |
| * Download chapters, read reviews, and order at: MinimalPerl.com * |
*----------------------------------------------------------------------*
S, Rajini (STSD) wrote:
> I am new to Perl Programming and have a query in perl.
>
> In perl is there any system defined functions to find out the Differences in dates.
>
> Eg :
>
> Date 1 -> 26-Jan-2009
> Date 2 -> 14-Jan-2009
>
> So the difference between two dates is 12 days.
There aren't any Perl built-in functions to do this. Fortunately there are a
whole range of Date modules on CPAN that might be suitable for your needs. You
will need to install whichever one(s) you choose on your system if they are not
already there. There is an answer to how to find out what is installed on your
system if you do:
perldoc -q installed
So, to find out what Date modules exist you'd search CPAN:
http://search.cpan.org/search?query=date&mode=all
In particular I'd suggest looking at Date::Simple
#!/usr/bin/perl -w
use strict;
use Date::Simple qw(date);
# Difference in days between two dates:
my $diff = date('2001-08-27') - date('1977-10-05');
If you need to convert from your date format '26-Jan-2009' to the format that
Date::Simple will accept then you might want to look at the Date::Parse module
as well.
There are other Date modules that might be better suited, and I encourage you to
look at them too.
All the best,
Jacinta
--
("`-''-/").___..--''"`-._ | Jacinta Richardson |
`6_ 6 ) `-. ( ).`-.__.`) | Perl Training Australia |
(_Y_.)' ._ ) `._ `. ``-..-' | +61 3 9354 6001 |
_..`--'_..-_/ /--'_.' ,' | con...@perltraining.com.au |
(il),-'' (li),' ((!.-' | www.perltraining.com.au |
I could find date formatter that represents date in various
Fomarts.
Is there a date Converter which converts the date,
Eg : given date 27-Jan-09, to be converted to 27.01.2009.
-Rajini
I'd suggest looking at Date::Parse, and strftime from the POSIX module.
strftime gets my vote for the least well documented function in the
Perl core module libraries (although this may be because I haven't
studied all of the functions available). If you don't have a Unix
system available, or a copy of the ANSI C standard, then you may find
the following page helpful to build your format string:
http://opengroup.org/onlinepubs/007908799/xsh/strftime.html
for example, assuming that @dateparts contains the correct elements,
then you could convert as follows:
use POSIX qw(strftime);
my $new_date = strftime("%d.%m.%Y", @dateparts);
# $new_date is now in the correct format.
Do read the documentation for strftime though (
http://perldoc.perl.org/POSIX.html#FUNCTIONS ) because there may be
format strings on the above mentioned page that are not portable.
Generally one would pass the results of calling localtime() to strftime eg:
# The current time in dd.mm.yyyy format:
use POSIX qw(strftime);
print strftime("%d.%m.%Y", localtime());
If Date::Parse can't help you build this array, then you might want to
have a look at Date::Manip. Be aware that Date::Manip is much bigger
and slower than the purpose built modules.
All the best,
Jacinta