我想使用perl以140220和130220的格式获取当前日期和完全去年的日期。
答案 0 :(得分:4)
也许以下内容会有所帮助:
use strict;
use warnings;
use Time::Piece;
my $time = Time::Piece->new;
my $currDate = $time->strftime('%y%m%d');
print $currDate, "\n";
my $lastYear = $time->add_years(-1)->strftime('%y%m%d');
print $lastYear;
输出:
140219
130219
答案 1 :(得分:3)
以下是使用DateTime的示例。
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use DateTime;
my $dt = DateTime->now();
my $year_ago = DateTime->now()->subtract(years => 1);
say $dt->strftime("%y%m%d");
say $year_ago->strftime("%y%m%d");