我需要一些关于perl日期计算的帮助,日期格式为"2012-02-03 00:00:00".特别是有一个工具,我可以用来增加天数,它正确切换到月份和年份?谢谢.
请参见DateTime.
#!/usr/bin/env perl
use strict; use warnings;
use DateTime;
my $ts = '2012-02-03 00:00:00';
my ($y, $m, $d) = ($ts =~ /([0-9]{4})-([0-9]{2})-([0-9]{2})/);
my $dt = DateTime->new(year => $y, month => $m, day => $d);
$dt->add( months => 2, days => 3 );
print $dt->strftime('%Y-%m-%d %H:%M:%S'), "\n";
使用DateTime :: Format类实际上有点干净,并且您可以免费获得错误检查.
use DateTime::Format::Strptime qw( );
my $format = DateTime::Format::Strptime->new(
   pattern   => '%Y-%m-%d %H:%M:%S',
   time_zone => 'local',
   on_error  => 'croak',
);
my $ts = '2012-02-03 00:00:00';
my $dt = $format->parse_datetime($ts);
$dt->add( months => 2, days => 3 );
print $format->format_datetime($dt), "\n";