in reply to convert date format from YYYY-MM-DD to YYYYMMDD
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: convert date format from YYYY-MM-DD to YYYYMMDD
by Perlseeker_1 (Acolyte) on May 16, 2014 at 16:05 UTC | |
by Your Mother (Archbishop) on May 16, 2014 at 17:36 UTC |