MLUG: [MLUG] formatting dates in perl
[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 Thu, 22 Mar 2001, Jonathan King came up with an efficient way to make
perl pump out a date (for days other than 'today') in the usual format.

> [EMAIL:PROTECTED ~]$ perl -le 'print scalar localtime'
> Thu Mar 22 16:35:16 2001
> [EMAIL:PROTECTED ~]$ perl -le 'print scalar localtime(time-86400)'
> Wed Mar 21 16:35:20 2001
> [EMAIL:PROTECTED ~]$ perl -le 'print "" . localtime(time-86400)'
> Wed Mar 21 16:35:32 2001

For example, to write a date for 39 days in the future, you can write
this:

perl -le 'print "" . localtime(time+60*60*24*39)'

That works great, but now I want a date in mm/dd/yy or mm/dd/yyyy format.
Does anyone know a good way to do that correctly?

I can do the following (multiline format for your reading convenience, but
I run it as a single line), but it isn't quite right...

perl -e
'($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
print "$mon/$mday/$year\n";'

...the problem with that code is that the $mon is 0..11 instead of 1..12,
and the $year = year - 1900.  So the date today is '3/1/101' instead of
4/1/01 or 4/1/2001.

I can do this:

perl -e
'($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
print "$mon $mday $year\n";' | awk '{print $1+1"/"$2"/"$3+1900}'

using awk to format the date properly.  I'll bet I just need to know how
to write the perl print statement correctly in the first place, then I can
skip the awk.  Can anyone tell me how?

Thanks in advance,

Mike

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

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