How to calculate a date a week ago from today

前端 未结 4 1405
终归单人心
终归单人心 2021-01-13 09:08

I wanna calculate the date a week ago from today with a specific format and put it in to a variable. For example, today is Nov 21st. 2014, and I wanna print out

4条回答
  •  抹茶落季
    2021-01-13 09:34

    DateTime version

    use DateTime;
    my $now = DateTime->now(time_zone => 'local')->subtract(weeks => 1);
    print $now->ymd, ' ',$now->hms;
    

    Date::Calc version

    Instead of one week you can subtract 7 days using Date::Calc module

    use Date::Calc qw(Add_Delta_Days);
    my @date = Add_Delta_Days( 2014, 11, 21, -7 );
    print join('-', @date);
    
    OUTPUT
        2014-11-14
    

提交回复
热议问题