Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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:26 UTC ( [id://614222]=perlquestion: print w/replies, xml ) Need Help??

naikonta has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

I provide a field for user to type in a date string, mostly in 'DD-MM-YYYY' format, and store it later in MySQL database, which uses 'YYYY-MM-DD' format. But sometimes, users use different separators such as slashes and dots. And in the edit form I have to display the date back to its original form. How do I do that? (Assume the input is in valid date range.)

Originally posted as a Categorized Question.

  • Comment on How do I convert date format between 'DD-MM-YYYY' and 'YYYY-MM-DD' back & forth?

Replies are listed 'Best First'.
Re: How do I convert date format between 'DD-MM-YYYY' and 'YYYY-MM-DD' back & forth?
by jettero (Monsignor) on May 08, 2007 at 19:41 UTC
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
    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 :-)
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:39 UTC
    The following answer uses regexes.
    # UK-style my $in_date = '30-04-2007'; (my $db_date = $in_date) =~ s/(\d\d)-(\d\d)-(\d\d\d\d)/$3-$2-$1/; (my $pr_date = $db_date) =~ s/(\d\d\d\d)-(\d\d)-(\d\d)/$3-$2-$1/; print "in: $in_date; db: $db_date; pr: $pr_date\n"; # in: 30-04-2007; db: 2007-04-30; pr: 30-04-2007
    # US-style my $in_date = '05-14-2007'; (my $db_date = $in_date) =~ s/(\d\d)-(\d\d)-(\d\d\d\d)/$3-$1-$2/; (my $pr_date = $db_date) =~ s/(\d\d\d\d)-(\d\d)-(\d\d)/$2-$3-$1/; print "in: $in_date; db: $db_date; pr: $pr_date\n"; # in: 05-14-2007; db: 2007-05-14; pr: 05-14-2007
    Instead of rolling your own, some CPAN modules might help, such as Date::Tiny, Date::Formatter, or Date::Simple. They look promising but I haven't tried them myself.
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:36 UTC
    Just in case you specify that the date must be in US-style (MM-DD-YYYY), then the above split won't work.
    my $in_date = '11-24-2007'; # or 11/24/2007, or 11.24.2007, or any non +-digit-separators my @parts = split /\D/, $in_date; my $db_date = join '-', @parts[2, 0, 1]; # to get the original format: my $sep = '-'; my @parts = split $sep, $db_date; my $pr_date = join $sep, @parts[1, 2, 0]; print "in: $in_date; db: $db_date; pr: $pr_date\n"; # in: 11-24-2007; db: 2007-11-24; pr: 11-24-2007

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (10)
As of 2024-04-16 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found