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: Last week is 2014-11-14
.
I know we can use Date::Calc
module, but I don't know how.
To calculate the number of weeks between two dates, start by counting the number of days between the start and end date. Then, divide that number by 7 days per week.
What'll you need to do is subtract today's date from the project start date. Excel has a TODAY function built in, which will save you from typing in the date in the correct format and continually updating as the days change. So, the formula is: =TODAY() – B2.
Check Time::Piece
and Time::Seconds
core modules,
use Time::Piece;
use Time::Seconds;
my $t = localtime() - ONE_WEEK;
print $t->ymd;
output
2014-11-14
use DateTime;
my $now = DateTime->now(time_zone => 'local')->subtract(weeks => 1);
print $now->ymd, ' ',$now->hms;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With