Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: date +%Y%m%d

by arturo (Vicar)
on Aug 21, 2001 at 17:29 UTC ( [id://106554]=note: print w/replies, xml ) Need Help??


in reply to Formatting date strings (was: date +%Y%m%d)

There are a few ways to do this in perl. It's a good day to learn about array slices and s/printf.

# this takes the 4th through sixth elements of the array # returned by localtime and sets @times to them my @times = (localtime)[3..5]; my $date = sprintf("%4d%02d%02d", $times[2]+1900, $times[1]+1, $times[ +0]);

The "%02d" formatting string (roughly: give me two digits, add enough '0's to the front to make the field two wide) is highly useful for 0-padding things to a specified length (in this case, 2).

Overall, it's more typing than the external call to date? Sure, but it's more portable and (although I haven't benchmarked it), more efficient (doesn't spawn a subshell).

HTH.

perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-18 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found