Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Answer: How can I format the output of localtime?

by jacques (Priest)
on Oct 21, 2004 at 18:34 UTC ( #401260=categorized answer: print w/replies, xml ) Need Help??

Q&A > dates and times > How can I format the output of localtime? - Answer contributed by jacques

This is an update of Roy Johnson's great answer, for those who just want the current time formatted in m/d/y.
my ($d,$m,$y) = (localtime)[3,4,5]; my $mdy = sprintf '%d/%d/%d', $m+1, $d, $y+1900;
Or if month and day should always be two digits:
my $mdy = sprintf '%02d/%02d/%04d', $m+1, $d, $y+1900;

Replies are listed 'Best First'.
Re: Answer: How can I format the output of localtime?
by johnnywang (Priest) on Oct 21, 2004 at 18:44 UTC
    if you want two digits for day/month like 04/03/2004, then
    my ($d,$m,$y) = (localtime(time))[3,4,5]; my $mdy = sprintf '%02d/%02d/%d', $m+1, $d, $y+1900;
      Update: Roy Johnson just notified me that localtime doesn't need any parameters to give the current time. On a different note, why aren't any of these replies appearing in the larger Q&A thread with the rest of the answers?
        Because Q&A answers are contributed content. Once they are posted, the maintainers of the section can edit or delete them at will, or combine several answers into one, etc. Given this, it doesn't make sense to show the replies which may no longer be relevant. They are still visible when looking at an individual answer, because normally you would be looking at a question and it's answers together.

        (That's my guess, or at any rate an argument for not changing it. It's equally likely to have originally been a bug.)

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2020-10-31 08:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (287 votes). Check out past polls.

    Notices?