为了与TMTOWTDI保持一致,您可以使用Time :: Piece
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $t = Time::Piece->strptime("20111230", "%Y%m%d");
print $t->strftime("%d-%b-%Y\n");
Run Code Online (Sandbox Code Playgroud)
如果我不能使用其中一个日期模块,POSIX并不是那么糟糕,它附带perl:
use v5.10;
use POSIX qw(strftime);
my $date = '19700101';
my @times;
@times[5,4,3] = $date =~ m/\A(\d{4})(\d{2})(\d{2})\z/;
$times[5] -= 1900;
$times[4] -= 1;
# strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
say strftime( '%d-%b-%Y', @times );
Run Code Online (Sandbox Code Playgroud)
制作@times有点难看.你不能总是得到你想要的东西,但如果你有时候尝试,你可能会发现你得到了你需要的东西.
一种方法是使用Date::Simple:
use warnings;
use strict;
use Date::Simple qw(d8);
my $d = d8('20111230');
print $d->format('%d-%b-%Y'), "\n";
__END__
30-Dec-2011
Run Code Online (Sandbox Code Playgroud)