Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Answer: How do I convert date format between 'DD-MM-YYYY' and 'YYYY-MM-DD' back & forth?

( #614227=categorized answer: print w/ replies, xml ) Need Help??

Q&A > dates and times > How do I convert date format between 'DD-MM-YYYY' and 'YYYY-MM-DD' back & forth? contributed by naikonta

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 :-)

Comment on Answer: How do I convert date format between 'DD-MM-YYYY' and 'YYYY-MM-DD' back & forth?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-09-18 11:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (112 votes), past polls