in reply to Question on date formatting

If the differences among machines/environments consistently fall into just those two possibilities (dd.mm.yyyy versus mm/dd/yyyy), then the following should do what you want:
s{ (\d{1,2}) ([./]) (\d{1,2}) \2 (\d{4}) \s+ (\d{1,2}):(\d{2}) } { my @a=($2 eq ".") ? ($4,$3,$1) : ($4,$1,$3); sprintf( "%s-%02d-%02d %02d:%s", @a,$5,$6 ) }ex;
The re-ordering of the first three fields depends on whether the field separators are periods or slashes, and the sprintf for the output format makes sure to include leading zeroes when necessary.

(updated to try improving legibility of the code -- not sure I succeeded)

Replies are listed 'Best First'.
Re^2: Question on date formatting
by KarthikK (Sexton) on Jun 20, 2008 at 08:21 UTC
    Thx..but how do i get your code working?
      Example:
      my @data = ( "blah blah 6/20/2000 4:56 foo bar baz\n", "bling blang 20.6.2000 4:56 flzb gzrk\n" ); for my $string ( @data ) { $string =~ s{ (\d{1,2}) ([./]) (\d{1,2}) \2 (\d{4}) \s+ (\d{1,2}):(\d{2}) } { my @a=($2 eq ".") ? ($4,$3,$1) : ($4,$1,$3); sprintf( "%s-%02d-%02d %02d:%s", @a,$5,$6 ) }ex; print $string; }
      That is, whatever string variable happens to contain one of the two types of date format, apply the lengthy "s{}{}" substitution statement to that string.

      UPDATE: Forgot to mention: if you have a string with more than one date in it (e.g. "from 1/2/2000 to 3/4/2000"), just change the "ex" at the end to "exg".