Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Date Extraction

by CombatSquirrel (Hermit)
on Dec 17, 2003 at 22:43 UTC ( [id://315406]=note: print w/replies, xml ) Need Help??


in reply to Date Extraction

perldoc -f localtime should help you; here is your code:
my @time = localtime; my $time = sprintf "%04d" . "%02d" x 5, $time[5] + 1900, # year $time[4] + 1, # month @time[3, 2, 1, 0]; # day, hour, minute, second print $time;

Hope this helped.
CombatSquirrel.

Update: Consider using one of the following modules:

1.) Time::Format. Example:
use Time::Format qw/%time/; my $time = $time{'yyyymmddhhmmss'}; print $time;
2.) Date::Format. Example:
use Date::Format; my $time = strftime "%Y%m%d%H%M%S", @{[localtime]}; print $time;
Cheers.
Entropy is the tendency of everything going to hell.

Replies are listed 'Best First'.
Re: Re: Date Extraction
by Aragorn (Curate) on Dec 18, 2003 at 07:35 UTC
    When dealing with localtime and similar functions, I'd like to name the elements that it returns explicitly:
    my ($sec, $min, $hour, $day, $mon, $year) = (localtime)[0..5]; my $time = sprintf "%04d" . "%02d" x 5, $year + 1900, $mon + 1, $day, $hour, $min, $sec; print $time;
    And presto! Self-documenting code :-)

    Arjen

      Obligatory mention of Time::Piece:
      use Time::Piece; my $t = localtime; printf "%.4d"."%.2d"x5, $t->year, $t->mon, $t->mday, $t->hour, $t->min +, $t->sec;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found