Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by naikonta (Curate)
on May 08, 2007 at 19:35 UTC ( [id://614227]=note: print w/replies, xml ) Need Help??


in reply to How do I convert date format between 'DD-MM-YYYY' and 'YYYY-MM-DD' back & forth?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://614227]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found