Well, basically, just reverse the date components in the list and put it back as string.
my $in_date = '30-04-2007';
my $db_date = join '-', reverse split '-', $in_date;
# to get the original format, do the same thing
my $pr_date = join '-', reverse split '-', $db_date;
print "in: $in_date; db: $db_date; pr: $pr_date\n";
# in: 30-04-2007; db: 2007-04-30; pr: 30-04-2007
For multi-possible-separator, use the non-digit character class for split().
my $in_date = '02/05/2007'; # or 02.05.2007, or any non-digit-separato
+rs
my $db_date = join '-', reverse split /\D/, $in_date;
# to get the original format, do the same thing as before
my $sep = '-';
my $pr_date = join $sep, reverse split '-', $db_date;
print "in: $in_date; db: $db_date; pr: $pr_date\n";
# in: 02/05/2007; db: 2007-05-02; pr: 02-05-2007
In this situation, there's no way to convert the date back with its original separator unless you kept it somewhere somehow, if that really matters. Besides, this might be a good chance to enforce your standard separator :-) |