Jeff Eggen wrote:
Tim Wolak <[EMAIL PROTECTED]> 06/01/2005 3:59:26 pm >>>

Hello all,

I am in need of a quick way to get the current date in the YYYYMMDD format. This is something I would like to understand better on how to

get the date with perl. Any suggestions would be most welcome.

As per the perlcheat page: "just use POSIX::strftime!"

# Begin
use POSIX qw(strftime);

my $date = strftime("%Y%m%d", localtime());
# End

There's possibly something in Date::Calc as well.  But this is the
quickest way I know, especially if you're familiar with the date command
in *nix.

Indeed, this is what I would use myself if I needed to format a unixtime string into a particular date format although I prefer %F myself ..


 $ perl -MPOSIX=strftime -le 'print strftime("%F", localtime())'
2005-01-07

<pedanticmode>
%F is the equivalent of %Y-%m-%d which, according to the strftime manpage, is the ISO 8601 date format.
</pedanticmode>


I've used this on occasion after returning values from Date::Parse, in order to insert the tested valid value in a database, later, but reformatted as the db expects to see it.

sub valid_birthday { #object-oriented method
    $_[0]->get_birthday() and
        return( str2time( $_[0]->{_birthday} ) );
    return undef;
}

and once I have a true value returned from the valid_birthday method, I can format it with POSIX strftime into what the database expects to see, as opposed to whatever the user typed into the form.

It's simple, elegant things like this that make me happy. :)

--
Scott R. Godin
Laughing Dragon Services
www.webdragon.net

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to