user595234
user595234

Reputation: 6259

perl how to get full month and date?

If today is Jan, 1, 2012, how to return a string of date "20120101' ?

sub getDateStr{
    my $date = shift;
    print $date->year.$date->month.$date->day."\n";
    return $date->year.$date->month.$date->day; 
}

Thanks

Upvotes: 1

Views: 2838

Answers (3)

runrig
runrig

Reputation: 6524

use Time::Piece;

$t = localtime()->strftime("%Y%m%d");

or simpler

$t = localtime()->ymd("");

Upvotes: 4

David Raab
David Raab

Reputation: 4488

use DateTime;
print DateTime->now->ymd("");
print DateTime->now->strftime("%Y%m%d");
print DateTime->now->format_cldr("yyyyMMdd");

Upvotes: 2

Axeman
Axeman

Reputation: 29854

From core Perl:

POSIX::strftime( '%Y%m%d', localtime );

Upvotes: 6

Related Questions