How do I get yesterday's date using localtime?

jdamae picture jdamae · Aug 17, 2010 · Viewed 42.9k times · Source

How do I tweak this to get yesterday's date using localtime?

use strict;

sub spGetCurrentDateTime;
print spGetCurrentDateTime;

sub spGetCurrentDateTime {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $currentDateTime = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900; #Returns => 'Aug 17 2010' 
return $currentDateTime;
}

~

Answer

daxim picture daxim · Aug 18, 2010
use DateTime qw();
DateTime->now->subtract(days => 1); 

The expression on the second line returns a DateTime object.