Here are four solutions, starting from the trivial (and arguably most fragile).
use feature 'say'; use DateTime::Format::Strptime qw(strptime strftime); use Time::Piece; my $raw_date = '2012-01-12'; # Using tr///; built into Perl. ( my $date = $raw_date ) =~ tr/-//d; say $date; # From Time::Piece; core since 5.10. $date = Time::Piece ->strptime( $raw_date, "%Y-%m-%d" ) ->ymd(''); say $date; # From DateTime::Format::Strptime; imported functions. $date = strftime( "%Y%m%d", strptime( "%Y-%m-%d", $raw_date ) ); say $date; # From DateTime::Format::Strptime; OO interface. $date = DateTime::Format::Strptime ->new( pattern => "%Y-%m-%d" ) ->parse_datetime($raw_date) ->ymd(''); say $date;
Obviously tr/// is a built-in Perl operator. Time::Piece comes with Perl 5.10 and newer as a core module. DateTime::Format::Strptime is purely a CPAN module that fits into the vast DateTime ecosystem.
Dave
In reply to Re: convert date format from YYYY-MM-DD to YYYYMMDD
by davido
in thread convert date format from YYYY-MM-DD to YYYYMMDD
by Perlseeker_1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |