Date
am 15.09.2004 23:58:48 von hastenthunderHi,
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.
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 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
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
"hastenthunder"
news:sW22d.1293$Ny6.2480@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;
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
> 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"
news:slrncllo81.c0c.keith@localhost.localdomain...
[corrected a top posted response - which is generally a sure sign of a poor
response]
> In article
>> 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);