在Perl中,可以使用內(nèi)置的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