Date format

Discussion in 'Perl Misc' started by cc96ai, Jul 23, 2008.

  1. cc96ai

    cc96ai Guest

    I want to change the date format in perl script

    input: "2005-11-01"
    output:November 1, 2005

    thanks
     
    cc96ai, Jul 23, 2008
    #1
    1. Advertising

  2. cc96ai wrote:
    > I want to change the date format in perl script
    >
    > input: "2005-11-01"
    > output:November 1, 2005


    The POSIX::strftime() function may be helpful.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 24, 2008
    #2
    1. Advertising

  3. cc96ai

    Carl Guest

    cc96ai <> writes:

    > I want to change the date format in perl script
    >
    > input: "2005-11-01"
    > output:November 1, 2005
    >
    > thanks


    You might be interested in the Date::Simple module:

    --example--
    $ cat test.pl
    #!/usr/bin/perl

    use strict;
    use warnings;

    use Date::Simple qw/date/;

    my $d = date('2005-11-01');
    print $d->format('%B %d, %Y'), "\n";

    __END__

    $ ./test.pl
    November 01, 2005
    $
    --end example--

    See:
    http://search.cpan.org/dist/Date-Simple/lib/Date/Simple.pm

    Hope that helps,
    Carl.
     
    Carl, Jul 24, 2008
    #3
  4. Greg Bacon wrote:
    >
    > use Date::parse qw/ strptime /;
    > use POSIX qw/ strftime /;
    >
    > # output:November 1, 2005
    > my $date = "2005-11-01";
    >
    > my(undef,undef,undef,$day,$month,$year) = strptime $date;
    > print strftime("%B %e, %Y\n", 0, 0, 0, $day, $month, $year);


    Or shorter:

    print strftime("%B %d, %Y\n", 0, 0, 0, (strptime $date)[3..5]);

    (%d is a more portable specifier than %e. The latter does not work on my
    Windows XP box.)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 25, 2008
    #4
  5. cc96ai

    cartercc Guest

    On Jul 23, 6:34 pm, cc96ai <> wrote:
    > I want to change the date format in perl script
    >
    > input: "2005-11-01"
    > output:November 1, 2005
    >
    > thanks


    If you want to roll your own, do this:

    %months = (
    1 => "January",
    2 => "February",
    3 => "March",
    etc )

    @input = split /-/, $input;
    printf "%s $d, $d", $months{$input[1]}, $input[2], $input[0];

    CC
     
    cartercc, Jul 25, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,172
    Janaka
    Feb 18, 2004
  2. DC
    Replies:
    5
    Views:
    3,009
    Brendan Reynolds
    Jul 1, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,279
    Michael Borgwardt
    May 30, 2004
  4. Chu
    Replies:
    3
    Views:
    4,194
    Shawn
    Aug 15, 2006
  5. Josh Sharpe
    Replies:
    1
    Views:
    213
    Brian Candler
    Sep 21, 2010
Loading...

Share This Page