Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

changing time date format.

by pglinx (Acolyte)
on Aug 22, 2006 at 02:07 UTC ( [id://568733]=perlquestion: print w/replies, xml ) Need Help??

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

I have 2 fields being sent by a form 1 is the date in mm/dd/yy 2 is military time. What is the simpilist way to convert it from 08/24/06 09:30 to Wednesday, August 24th 9:30am I seem to be coming on to alot of very complicated ways I like simple...

Replies are listed 'Best First'.
Re: changing time date format.
by explorer (Chaplain) on Aug 22, 2006 at 03:25 UTC

    A solution with the big DateTime module family

    #!/usr/bin/perl -l # 08/24/06 09:30 => Thursday, August 24th 9:30am use DateTime::Format::Strptime; my $format_input = '%D %H:%M'; my $format_output = '%A, %B %eth %l:%M%P'; my $Strp = new DateTime::Format::Strptime( pattern => $format_input, locale => 'en_US', time_zone => 'America/New_York', ); ## Start my $date_time = '08/24/06 09:30'; # 1. Get the date/tim +e my $dt = $Strp->parse_datetime( $date ); # 2. Convert to DateT +ime object my $newtime = $dt->strftime( $format_output ); # 3. Output the new d +ate/time print $newtime; ## End
    Updated: Solution with the Date::Format and Date::Parse modules
    #!/usr/bin/perl -l # 08/24/06 09:30 => Thursday, August 24th 9:30am use Date::Parse; use Date::Format; use Date::Language; my $format = '%A, %B %o %l:%M%P'; my $language = Date::Language->new('English'); ## Start my $date_time = '08/24/06 09:30'; my $time = $language->str2time( $date_time ); # 1. Parse my $newtime = $language->time2str( $format, $time ); # 2. Output print $newtime; ## End
      I used the updated solution but for some reason I get Thursday, August 24th 9:30P I get P for every time instead of am or pm Thanks

        What operating system are you using? Support for the %P format code isn't universal. Take a look at the documentation for "strftime" on your system.

        You could try using %p instead. Or, alternatively, just use the 24 hour clock (%k in place of %l in the output format string) - that's far more readable in my opinion.

        Personally, I'd use the DateTime modules to solve this problem.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

      Could you tell me which Date module this is exactly there seems to be many different ones out there.
Re: changing time date format.
by swampyankee (Parson) on Aug 22, 2006 at 02:21 UTC

    Fairly similar questions have been asked quite often. I believe the generic answer is Date::Calc.

    emc

    Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.

    Albert Einstein
      i agree wholeheartedly with your sig. our software is becoming more and more sophisticated, and we've learned from the Y2K dramas....so why don't U.S. programmers learn once and for all to store dates in a database in international format, and apply U.S./european/other display format for display purposes only??!! I can't believe that the global company i work for still manages to churn out code in U.S. that only takes U.S. date format into consideration...WAKE UP drongos....not you..them.
      the hardest line to type correctly is: stty erase ^H

        Yeah!

        I worked with NASA and all was simple... ALL files had date/times into their names in ISO8601 format...

        Only run your favorite editor and to write:

        use DateTime::Format::ISO8601;
Re: changing time date format.
by imp (Priest) on Aug 22, 2006 at 02:38 UTC
    Date::Calc offers a huge variety of options for parsing and manipulating dates. This is good in that it can almost always do what you want.. and bad because the piece you want is buried among the 200 other features.

    For a simple version you could also use split and strftime (provided by POSIX.

    use POSIX qw(strftime); use strict; use warnings; my $date = '08/24/06'; my $time = '09:30'; my ($month,$day,$year) = split /\//, $date; my ($hour,$min) = split /:/, $time; my $formatted = strftime("%A, %B %d %I:%M %p",0,$min,$hour,$day,$month +,$year); print $formatted, "\n";
Re: changing time date format.
by jdtoronto (Prior) on Aug 22, 2006 at 02:59 UTC
Re: changing time date format.
by Zaxo (Archbishop) on Aug 22, 2006 at 02:26 UTC

    There's a lot to be said for a unified timestamp, in unix epoch time.

    After Compline,
    Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://568733]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found