Date

Hi,

If I have a date variable, how can I output it in the format of 'YYYYMMDD'?

printf ("%8s", "$perl_date"); just returns the $perl_date in DD-MON-YY
format.

Thanks

G.
hastenthunder [ Mi, 15 September 2004 23:58 ] [ ID #18636 ]

Re: Date

hastenthunder wrote:
> If I have a date variable, how can I output it in the format of
> 'YYYYMMDD'?
>
> printf ("%8s", "$perl_date"); just returns the $perl_date in
> DD-MON-YY format.

Home-work time?

If $perl_date contains a string such as '15-SEP-04', that sounds
plausible. What made you hope that the printf() function would
magically convert the string to the format you want?

You need to parse the string with e.g. split() or a regex, and convert
the month to its order number. A module such as Date::Parse might be
helpful for this.

After that, printf() or sprintf() may be useful for getting the
desired format.

Good luck!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Hjalmarsson [ Do, 16 September 2004 00:30 ] [ ID #18637 ]

Re: Date

hastenthunder wrote:

> Hi,
>
> If I have a date variable, how can I output it in the format of 'YYYYMMDD'?
>
> printf ("%8s", "$perl_date"); just returns the $perl_date in DD-MON-YY
> format.
>
> Thanks
>
> G.
>
>

Check out the UnixDate function of Date::Manip

SMO
SMO [ Do, 16 September 2004 01:36 ] [ ID #18638 ]

Re: Date

"hastenthunder" <hastenthunder [at] hotmail.com> wrote in message
news:sW22d.1293$Ny6.2480 [at] mencken.net.nih.gov...
> Hi,
>
> If I have a date variable, how can I output it in the format of
'YYYYMMDD'?
>
> printf ("%8s", "$perl_date"); just returns the $perl_date in DD-MON-YY
> format.

Set $perl_date to YYYMMDD instead of DD-MON-YY ;-)

Seriously

use POSIX 'strftime';
my $perl_date = strftime "%Y%m%d",localtime;
Tintin [ Do, 16 September 2004 12:27 ] [ ID #18640 ]

Re: Date

Try using localtime.
----------------------8<----------------------------
my ($sec, $min, $hour, $day, $month, $year) = (localtime)[0,1,2,3,4,5];

$month += 1;
$year = sprintf("%02d", $year % 100);
----------------------8<----------------------------


In article <sW22d.1293$Ny6.2480 [at] mencken.net.nih.gov>, hastenthunder wrote:
> Hi,
>
> If I have a date variable, how can I output it in the format of 'YYYYMMDD'?
>
> printf ("%8s", "$perl_date"); just returns the $perl_date in DD-MON-YY
> format.
>
> Thanks
>
> G.
>
>
Keith Meidling [ Mi, 29 September 2004 18:21 ] [ ID #18667 ]

Re: Date

"Keith Meidling" <keith [at] home.com> wrote in message
news:slrncllo81.c0c.keith [at] localhost.localdomain...
[corrected a top posted response - which is generally a sure sign of a poor
response]

> In article <sW22d.1293$Ny6.2480 [at] mencken.net.nih.gov>, hastenthunder wrote:
>> Hi,
>>
>> If I have a date variable, how can I output it in the format of
>> 'YYYYMMDD'?
>>
>> printf ("%8s", "$perl_date"); just returns the $perl_date in
>> DD-MON-YY
>> format.
>
> Try using localtime.
> ----------------------8<----------------------------
> my ($sec, $min, $hour, $day, $month, $year) = (localtime)[0,1,2,3,4,5];
>
> $month += 1;
> $year = sprintf("%02d", $year % 100);
> ----------------------8<----------------------------

Please explain how your code even comes close to answering the OP's question
of converting DD-MON-YY to YYYYMMDD

Also, why create variables that never get used (ie: $sec,$min,$hour).
That's wasteful.

*If* the OP wanted to get the current date in YYYYMMDD format, they could
have done

my ($day,$mon,$year) = (localtime)[3..5];
printf "%d%02d%02d\n",$year+1900,$mon+1,$day;

or

use POSIX 'strftime';
print strftime("%Y%m%d",localtime);
Tintin [ Do, 30 September 2004 10:00 ] [ ID #18668 ]
Perl » alt.perl » Date

Vorheriges Thema: migrating from php to perl
Nächstes Thema: Perl Parsing