MLUG: Re: [MLUG] formatting dates in perl
Re: [MLUG] formatting dates in perl
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Sun, 1 Apr 2001, Mike Miller wrote:

> On Sun, 1 Apr 2001, Mikhail Kovalenko wrote:
> 
> > How about this:
> >
> > perl -e
> > '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
> > print $mon+1," $mday ",$year+1900,"\n";'
> 
> Thanks, Mikhail!  Yes, that worked well.  I just don't understand how
> quoting, commas and brackets work in perl print statements.  Your code
> was illuminating.  After reading it, I altered the line of code as
> follows...
> 
> perl -e
> '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
> print $mon+1,"/$mday/",$year+1900,"\n";'
> 
> ...so that I'd get 4/1/2001 instead of 4 1 2001.

Hmm, I thought you wanted mm/dd/yy?  In which case, how about:

  perl -le 'use POSIX; print strftime("%D",localtime);'
  04/01/01

If you want mm/dd/yyyy, then that should be:

  perl -le 'use POSIX; print strftime("%m/%d/%Y",localtime);'
  04/01/2001

Note that strftime can do a lot more than this for you; see:

   man 3 strftime

for many other options.  

Oh yeah: the most succinct way to print out mm/dd/yy for the current time
that I know of is:

   perl -lMPOSIX -e 'print strftime("%D",localtime);'
   04/01/01

jking


--
To manage your subscription, go to http://mlug.missouri.edu/members/edit.php

Archives are available at http://mlug.missouri.edu/list-archives/