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

Hello all,

Is there a perl routine or module available that would allow me to accept a date in any format and return a specific format? The input value is a string and I only need to output a string, as well. I have found various modules, but they seem to require a localtime object as input.

Thanks!

Replies are listed 'Best First'.
Re: Format user entered date?
by moritz (Cardinal) on Aug 31, 2009 at 20:18 UTC
    Maybe Date::Parse could be of help to you?
    Perl 6 - links to (nearly) everything that is Perl 6.

      Thanks. I've tried using Date::Parse but I was getting output similar to this:

      $fulltime = $fields[2] . " " . $fields[3]; $str2time = str2time($fulltime);
      .. and the output ..
      FULLTIME 7/20/2009 21:32:27
      STR2TIME: 1248143547
      
      Thanks for any guidance!

        So you need to format your output too. That can be done with a combination of localtime and POSIX::strftime for example.

        Perl 6 - links to (nearly) everything that is Perl 6.
Re: Format user entered date?
by Sue D. Nymme (Monk) on Aug 31, 2009 at 20:46 UTC

    Date::Manip is good for accepting input in just about any conceivable format. It's big and slow, however.

    If you have a specific input/output format in mind, your best bet is to use DateTime and one of the many DateTime::Format modules. If none of them suit you, you can roll your own patterns with Regexp::Common::time, and you can use Time::Format to output dates/times in any format you like.

Re: Format user entered date?
by astroboy (Chaplain) on Sep 01, 2009 at 00:40 UTC
    #!/usr/local/bin/perl use DateTime::Format::Natural; my @dates = ('Yesterday', '21st August 2009', '2009-07-17 2:35pm'); my $output_format = '%d/%m/%Y %H:%M:%S'; foreach my $date (@dates) { my $parser = DateTime::Format::Natural->new; my $dt = $parser->parse_datetime($date); print $dt->strftime($output_format), "\n";; }
    Output
    31/08/2009 00:00:00 21/08/2009 00:00:00 17/07/2009 14:35:00