in reply to Re: Convert date format in database table.
in thread Convert date format in database table.

Why use a regex when split will do? :)
my $date = '2/3/1906'; my $new_date = sprintf("%04d-%02d-%02d 00:00:00",(split /\//, $date)[2 +,1,0]); # $new_date is "1906-03-02 00:00:00"

cLive ;-)

Update: As tachyon points out, I misread the question - the phrase "must be all zeroes" threw me :)

But again, I still say split :)

my $new_date = sprintf("%04d-%02d-%02d %02d:%02d:00",(split /\D+/, $da +te)[2,1,0,3,4]);

:)

Replies are listed 'Best First'.
Re: Re: Re: Convert date format in database table.
by tachyon (Chancellor) on Oct 07, 2003 at 06:18 UTC

    So you deal with the HH:MM (which would require a split/[:/ ]/, and don't have to parse the CSV of which this date string was stated to be a field.....

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Re-reading the question :)

      'CCYY-MM-DD hh:mm:ss' where all zeroes are required

      Ah, OK, let's re-read that so it makes sense - "where leading zeroes are required". LOL.

      Touché

      cLive ;-)

        The very first Christmas 0000-00-00 00:00:00 might be a literal interpretation ;-)

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Re: Re: Convert date format in database table.
by runrig (Abbot) on Oct 09, 2003 at 01:57 UTC
    Why use a regex when split will do?
    Because a regex allows for more precise format matching (year can be '\d{4}', the rest can be '\d{1,2}) in case, e.g., we have badly formatted dates and we don't want errors upon inserting to the database. I know, it's not necessarily a great reason, but it's a reason :-)