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]
Mike Miller wrote:
> 
> 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?

Get /bin/date from sh-utils-2.0 instead.

$ man date

NAME	date - print or set the system date and time
SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [OPTION] [MMDDhhmm[[CC]YY][.ss]]
DESCRIPTION
       Display the current time in the given FORMAT, or set the system
date.

...[snip]...

FORMAT controls the output.  The only valid option for the second form
specifies Coordinated  Universal Time.  Interpreted sequences are:
       %%     a literal %
       %a     locale's abbreviated weekday name (Sun..Sat)
...[snip many wonderful sequences]...

So your date in mm/dd/yyyy format becomes

$ date -d '39 days' +'%x'
05/10/2001

and in a string of perl:

$ perl -e "print \"`date -d '39 days' +'%x'`\n\""   ;-)

-- MK
<< Terra es et in terram ibis >>
--
To manage your subscription, go to http://mlug.missouri.edu/members/edit.php

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