在Perl中,可以使用内置的DateTime模块来处理日期和时间。以下是一些常见的操作:
use DateTime;
my $dt = DateTime->now;
print $dt->datetime; # 输出当前日期和时间
use DateTime;
my $dt = DateTime->now;
print $dt->strftime('%Y-%m-%d %H:%M:%S'); # 格式化日期和时间
use DateTime;
my $dt = DateTime->now;
$dt->add(days => 1); # 增加一天
$dt->subtract(hours => 3); # 减去3小时
use DateTime;
my $dt1 = DateTime->new(year => 2021, month => 6, day => 1);
my $dt2 = DateTime->new(year => 2021, month => 6, day => 2);
if ($dt1 < $dt2) {
print "dt1在dt2之前\n";
}
use DateTime;
my $dt1 = DateTime->new(year => 2021, month => 6, day => 1);
my $dt2 = DateTime->new(year => 2021, month => 6, day => 5);
my $diff = $dt2 - $dt1;
print "日期差: $diff\n"; # 输出4
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: Perl与R语言怎么集成使用