Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

date conversion

by sathya83aa (Acolyte)
on Jul 18, 2013 at 05:57 UTC ( [id://1044972]=perlquestion: print w/replies, xml ) Need Help??

sathya83aa has asked for the wisdom of the Perl Monks concerning the following question:

Hi all..

I have a variable in mmddyy format. But I would like to convert it to mm/dd/yyyy format.

Please help me in this regard.

Thanks & Regards,

Sathya V.

Replies are listed 'Best First'.
Re: date conversion
by Rahul6990 (Beadle) on Jul 18, 2013 at 06:34 UTC
    Hi ,

    One option is to use regex:
    my $in_date = '051407'; (my $db_date = $in_date) =~ s/(\d\d)(\d\d)(\d\d)/$1-$2-$3/; print ":$db_date:";

    But there is one issue how would you find that date belongs to 20th century or 21st
    Example:
    The above date could be :05-14-2007 or 05-14-1907

    And one advise its always better to search before you post a question.
Re: date conversion
by hdb (Monsignor) on Jul 18, 2013 at 07:07 UTC

    You could use substr to insert the slashes and the century into your data as it is already in the correct basic format. For example, substr $data, 2, 0, "/"; would insert a slash after the month into your date. Whether to insert "/20" or "/19" before the year still needs to be decided based on the last two digits of your date (or any other assumption you are willing to make).

Re: date conversion
by Loops (Curate) on Jul 18, 2013 at 07:14 UTC
    use Date::Manip; my $in = '051093'; print UnixDate($in =~ s#(..)(..)#$1/$2/#r, '%m/%e/%Y');
Re: date conversion
by mtmcc (Hermit) on Jul 18, 2013 at 08:25 UTC
    How are you getting your date originally (mmddyy)?

    -Michael

Re: date conversion
by Anonymous Monk on Jul 18, 2013 at 07:06 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1044972]
Approved by Happy-the-monk
Front-paged by toolic
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: (4)
As of 2024-04-23 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found