# The date and target (1 is Monday, 7 Sunday) my $dt = DateTime->new(year => 1998, month => 4, day => 3); # Friday my $target = 6; # Saturday # Get the day of the week for the given date my $dow = $dt->day_of_week(); # Apply the corrections my ($prev, $next) = ($dt->clone(), $dt->clone()); if ($dow == $target) { $prev->add( days => -7 ); $next->add( days => 7 ); } else { my $correction = ( $target - $dow + 7 ) % 7; $prev->add( days => $correction - 7 ); $next->add( days => $correction ); } # $prev is 1998-03-28, $next is 1998-04-04