Perl: Easy way to rearrange MM/DD/YYYY to YYYY-MM-DD?
Example input: 8/23/2008 (single digit day/month 开发者_如何学Cdo not get leading zeros)
Desired output: 2008-08-23
Prefer to use perl. Thanks!
Use DateTime::Format::Strptime
and DateTime
(which everyone should have installed if they have to deal with dates or times in any way):
my $parser = DateTime::Format::Strptime->new(pattern => '%m/%d/%Y');
my $iso_date = $parser->parse_datetime('8/23/2008')->ymd('-');
sprintf '%3$04d-%02d-%02d', split m:/:, $inputDate
If you haven't yet installed DateTime, you can use Time::Piece:
use Time::Piece;
my $t = Time::Piece->strptime('8/23/2008', '%m/%d/%Y');
print $t->strftime('%Y-%m-%d'),"\n";
join '-', (split qr{/}, $_)[2,0,1]
split
join
- list slices
精彩评论