大佬教程收集整理的这篇文章主要介绍了我怎样才能在Perl中获得本周的日期?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/usr/bin/env perl use warnings; use Strict; use datetiR_14_11845@e; # Calculate numeric value of today and the # target day (Monday = 1,Sunday = 7); the # target,in this case,is Monday,since that's # when I want the week to start my $today_dt = datetiR_14_11845@e->now; my $today = $today_dt->day_of_week; my $target = 1; # Create datetiR_14_11845@e copies to act as the "bookends" # for the date range my ($start,$end) = ($today_dt->clone(),$today_dt->clone()); if ($today == $target) { # If today is the target,"start" is already set; # we simply need to set the end date $end->add( days => 6 ); } else { # Otherwise,we calculate the Monday preceeding today # and the Sunday following today my $delta = ($target - $today + 7) % 7; $start->add( days => $delta - 7 ); $end->add( days => $delta - 1 ); } # I clone the datetiR_14_11845@e object again because,for some reason,# I'm wary of using $start directly... my $cur_date = $start->clone(); while ($cur_date <= $end) { my $date_ymd = $cur_date->ymd; print "$date_ymd\n"; $cur_date->add( days => 1 ); }
如上所述,这是有效的,但它是最快还是最有效的?我猜测速度和效率可能不一定在一起,但您的反馈非常感谢.
但是,这假设星期一是一周的第一天.星期天,从…开始
@H_74_2@my $start_of_week = datetiR_14_11845@e->today() ->truncate( to => 'week' ) ->subtract( days => 1 );无论哪种方式,最好使用truncate方法而不是重新实现它,就像friedo所做的那样;)
以上是大佬教程为你收集整理的我怎样才能在Perl中获得本周的日期?全部内容,希望文章能够帮你解决我怎样才能在Perl中获得本周的日期?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。