Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Date format manipulation

by Anonymous Monk
on Oct 17, 2001 at 00:13 UTC ( [id://119239]=perlquestion: print w/replies, xml ) Need Help??

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

I need to modify the format of a date that I am getting. I'm sure there are at least a few mods out there that can do this easily so any suggestions will be greatly appreciated. Here's the details.

I'm getting a "last updated" day and time from an external data source in one of my scripts.

It comes in the following format:
"10/16/2001 3:26PM; ET"

I need to display it in the following format:
"Tue Oct 16 3:26 PM ;ET"

Anyone know of a mod or a good script where I can pass the value of a date in the format of "mm/dd/yy" and it returns the value of "dow mon dd"? (dow=Day of week 3 letter abbreviation, mon=month 3 letter abbreviation, dd=date)

Also, I need to parse the "3:26PM" variable so that I can add a space between the "3:26" and the "PM". Any and all help is appreciated. I know this should be simple, but it's been causing me a lot of trouble.

Thanks,
Wood

Replies are listed 'Best First'.
Re: Date format manipulation
by blakem (Monsignor) on Oct 17, 2001 at 00:51 UTC
    How about something like this:
    #!/usr/bin/perl -wT use strict; use POSIX qw(strftime); my $date = '10/16/2001 3:26PM; ET'; my $newdate = dateconvert($date); print "'$date' => '$newdate'\n"; sub dateconvert { my $date = shift; #parse the date my ($mon,$day,$year,$hour,$min,$ampm,$tz) = $date =~ m|(\d+)/(\d+)/(\d+)\s+(\d+):(\d+)\s*(\w+)\s*;\s*(\w+)|; # munge some vars to get them into 'unix standard form' $mon--; #ala, months from -0-11 instead of 1-12 $year-=1900; #and years being offshifted by 1900 $hour+=12 if $ampm =~ /pm/i; my $sec = 0; # format our new date my $newdate = strftime("%a %b %d %I:%M%p ;$tz",$sec,$min,$hour,$day,$mon,$year); $newdate =~ s/0(\d:)/$1/; # convert 03:26 into 3:26... ugly, sorry +. return $newdate; } =OUTPUT '10/16/2001 3:26PM; ET' => 'Tue Oct 16 3:26PM ;ET'

    -Blake

Re: Date format manipulation
by projekt21 (Friar) on Oct 17, 2001 at 12:15 UTC

    As Date::Manip is very slow, you maybe want to try Date::Parse for parsing the date and either printf or Date::Format for a formatted output:

    use Date::Parse; # get a time() compatible value my $time = str2time("...string..."); # or an array my ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime("...string..."); $month++; $year += 1900;
    and:
    use Date::Format; my $format = "%a %b %d %l:%M %p ;%Z"; print time2str($format, time);

    Hope this helps.

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste

Re: Date format manipulation
by Masem (Monsignor) on Oct 17, 2001 at 00:22 UTC
    The module Date::Manip is your friend; it's got a very flexiable date/time string recognizition engine that will accept practically any variation that you can think of, and then you can break out exactly what you need, both individual parts and as specified formats for your needs.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Date format manipulation
by boo_radley (Parson) on Oct 17, 2001 at 00:20 UTC
Re: Date format manipulation
by scain (Curate) on Oct 17, 2001 at 00:21 UTC
    just a wild guess here, but try Date::Manip.

    Scott (with just the tiniest bit of sarcasm)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2024-04-19 00:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found