Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Creating a string for a date stamp

by dthacker (Deacon)
on Sep 20, 2001 at 00:33 UTC ( #113467=perlquestion: print w/replies, xml ) Need Help??

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

I've have a program that copies files from a log directory to an archive directory and appends a date stamp to the end of the filename. After looking at some other code locally I used this sub to create the datestamp:
sub get_time_stamp() { my @tan=Today_and_Now(); my $stamp=join '', @tan[0..4]; return $stamp; }

Unfortunately this is giving me YYYYMDDHHMM instead of YYYYMMDDHHMM. Is there a more effective way of getting the string I want than padding the output of  Today_and_Now?

Replies are listed 'Best First'.
Re: Creating a string for a date stamp
by danboo (Beadle) on Sep 20, 2001 at 00:39 UTC
    i'd recommend sprintf here:
    sub get_time_stamp() { my @tan=Today_and_Now(); my $stamp= sprintf '%d%02d%02d%02d%02d', @tan[0..4]; return $stamp; }
    or the brief version:
    sub get_time_stamp() { sprintf '%d%02d%02d%02d%02d', (Today_and_Now())[0..4]; }
    this will keep all all the potential 1 or 2 digit fields padded with a zero. cheers, - danboo
Re: Creating a date stamp (Russ: POSIX::strftime)
by Russ (Deacon) on Sep 20, 2001 at 01:19 UTC
    Look at POSIX::strftime. You provide a format string (similar to sprintf) which would look very similar to the format strings you gave as examples :-)

    Russ
    Brainbench 'Most Valuable Professional' for Perl

Re: Creating a string for a date stamp
by mitd (Curate) on Sep 20, 2001 at 05:08 UTC
    good old mitd's fav' Date::Format. It is your friend.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-07-06 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?