----- Original Message -----
From: ""michael wang"" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: <beginners@perl.org>
Sent: Wednesday, August 06, 2008 9:00 AM
Subject: date info
Hi,
if I have something like xxxx20080503, how can I get the date a year ago,
such as xxxx20070503 in perl?
Thanks,
michael
There are many date modules on CPAN such as Date::Calc, DateTime, to name
two. They may be overkill for the simple task you need to solve, but they
will prevent errors, in this case, concerning leap years.
xxxx20080229
xxxx20070229 * wrong
Here's an example using the DateTime module. It will correctly account for
leap years.
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
my $str = 'xxxx20080229';
$str =~ /^(.+)(\d{4})(\d\d)(\d\d)$/;
my $date = DateTime->new( year => $2,
month => $3,
day => $4,
);
print $1, $date->ymd(''), "\n";
$date->subtract( years => 1 );
print $1, $date->ymd(''), "\n";
__END__
*** prints
xxxx20080229
xxxx20070228
Chris
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/