Perl 解析字符串
perl语言中的库 Time::Piece可以将字符串解析为日期对象。
use Time::Piece;my ($y, $m, $d) = '20230823' =~ /^([0-9]{4})([0-9]{2})([0-9]{2})\z/ or die;my $dt = Time::Piece->strptime("$y$m$d","%Y%m%d");
Perl获取多天之前的日期
计算日期间的差值需要Time::Seconds库
支持了以下单位进行计算
ONE_WEEK, ONE_DAY, ONE_MONTH, ONE_SECOND等
use Time::Piece;my ($y, $m, $d) = '20230823' =~ /^([0-9]{4})([0-9]{2})([0-9]{2})\z/ or die;my $dt = Time::Piece->strptime("$y$m$d","%Y%m%d");my $before = $dt - ONE_DAY * 30; #计算30天前的日期my $before_str = $before->strftime("%Y%m%d");print(" $before_str \n");