http://www.perlmonks.org?node_id=1064236


in reply to Setting up date to subtract specific number from it

use 5.014; use DateTime qw(); my %day2t = ( Friday => 'T', Saturday => 'T-1', Sunday => 'T-2', Monday => 'T-3', Tuesday => 'T-4', Wednesday => 'T-5', Thursday => 'T-6', ); { my $today = DateTime->now->truncate(to => 'day'); # 2013-11-25T00: +00:00 $today->day_name; # Monday $day2t{$today->day_name}; # T-3 my $into_the_past = ($day2t{$today->day_name} =~ s/T-?//r) || 0; # + 3 $today->clone->subtract(days => $into_the_past)->strftime('%F'); # + 2013-11-22 } { my $beginning_of_november = DateTime->new(year => 2013, month => 1 +1, day => 1); # 2013-11-01T00:00:00 $beginning_of_november->day_name; # Friday $day2t{$beginning_of_november->day_name}; # T my $into_the_past = ($day2t{$beginning_of_november->day_name} =~ s +/T-?//r) || 0; # 0 $beginning_of_november->clone->subtract(days => $into_the_past)->s +trftime('%F'); # 2013-11-01 }

Replies are listed 'Best First'.
Re^2: Setting up date to subtract specific number from it
by vihar (Acolyte) on Nov 25, 2013 at 16:25 UTC
    I really appreciate your help! Just wondering if there is a way to do this without using the Date::Time module? I am not a root user on my box and I am supposed to be avoiding the use of modules as much as possible. Thanks!

      "...as much as possible."

      Good, so they left you a little room to work with. I mean "as much as possible" isn't as restrictive as "none at all, ever, for any reason."

      Date manipulation is one of the situations where modules are invaluable. Hand rolled solutions usually miss something. Do they want date bugs, or date modules?

      Perhaps you could ask your client what date modules they already have installed, and work using one of those.


      Dave