Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Date Convert

by Anonymous Monk
on Jan 09, 2013 at 19:05 UTC ( #1012545=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have date format like this: "01/09/2012" that is getting inserted into sql server, I am getting errors, I am trying to convert into a sql format, something like this:
... my $date = "01/09/2012"; $date = strftime '%Y-%m-%d %H:%M:%S', $date; print $date; ...

Any idea on how I could do this?

Replies are listed 'Best First'.
Re: Date Convert
by roboticus (Chancellor) on Jan 09, 2013 at 19:26 UTC

    If it's a fixed format input string, I wouldn't bother with strftime. I'm guessing that something as simple as the following should work:

    my $date = "01/09/2012"; $date = join("-", reverse split /\//, $date), " 12:00:00";


    When your only tool is a hammer, all problems look like your thumb.

Re: Date Convert
by Anonymous Monk on Jan 09, 2013 at 20:42 UTC
    Use a date/time Perl object type that is friendly to DBI and specify the date value as a parameter to the query, so that DBI will recognize that the parameter is a binary value of date/time type and will bind to the parameter accordingly. Don't present it as a character string if you can avoid it. (Strange things can happen such as code that works in America but not in Europe where the "locale" is different: is "01/09/2012" January 9th or September 1st?)
      Thats the issue, to convert a string like "01/09/2012" into this date/time Perl object type that is friendly to DBI like you mentioned.

      AM: your post sounds like the right thing. Can you please give a little code example, how to do it?

      Many TIA

Re: Date Convert
by mhearse (Chaplain) on Jan 09, 2013 at 21:34 UTC
    Why not use epoch? Can be handled with a unsigned int data type.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012545]
Approved by blue_cowdawg
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-06-19 02:03 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.