Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: changing time date format.

by explorer (Chaplain)
on Aug 22, 2006 at 03:25 UTC ( #568749=note: print w/ replies, xml ) Need Help??


in reply to changing time date format.

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


Comment on Re: changing time date format.
Select or Download Code
Re^2: changing time date format.
by pglinx (Acolyte) on Aug 22, 2006 at 12:57 UTC
    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

        Ok, I got it to work, one more question For some reason I can run it from the shell but when I incorporate it into my script it crashes it. and only crashes it from a browser, what am I missing?
        { 
        use Date::Parse;
        use Date::Format;
        use Date::Language;
        
         $format    = '%A, %B %o %l:%M%p';
         $language  = Date::Language->new('English');
        
        ## Start
         $date_time = '08/24/06 09:30';
         $time      = $language->str2time( $date_time );     # 1. Parse
         $newtime   = $language->time2str( $format, $time ); # 2. Output
        
        {
        print "$newtime\n";
        }
        }
        
        
Re^2: changing time date format.
by pglinx (Acolyte) on Aug 24, 2006 at 13:59 UTC
    Could you tell me which Date module this is exactly there seems to be many different ones out there.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://568749]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-12-18 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (47 votes), past polls