How can I get this week's dates in Perl?

后端 未结 4 1753
感情败类
感情败类 2021-02-09 04:44

I have the following loop to calculate the dates of the current week and print them out. It works, but I am swimming in the amount of date/time possibilities in Perl and want t

4条回答
  •  有刺的猬
    2021-02-09 04:52

    You can use the DateTime object to get the current day of the week as a number ( 1-7 ). Then just use that to find the current week's Monday. For example:

    my $today = DateTime->now;
    my $start = $today->clone;
    
    # move $start to Monday
    $start->subtract( days => ( $today->wday - 1 ) );   # Monday gives 1, so on monday we
                                                        # subtract zero. 
    
    my $end = $start->clone->add( days => 7 );
    

    The above is untested but the idea should work.

提交回复
热议问题