Help - Search - Member List - Calendar
Full Version: perl program to convert system date to yymmdd
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Nishi Prafull
HI:

I want to write a perl script that would compute the date in the
format yymmdd(050303) and subsitute it for a variable in the perl
script. This variable is thereafter subsituted in a command that will
be run inside the script.

myTest.pl

var aDate;
get todays date and convert it to yymmdd and assign it to aDate
subsitute in a command such as
$cmd = "integrate -t push -l $aDate"
system($cmd);

I will be running this script on unix. But I would like to be able to
get the date value platform independant.

Thanks.

Nishi Prafull wrote:
QUOTE
HI:

I want to write a perl script that would compute the date in the
format yymmdd(050303) and subsitute it for a variable in the perl
script. This variable is thereafter subsituted in a command that will
be run inside the script.

myTest.pl

var aDate;
You can do it a number of ways, here is a start on one way:


my @MyDateTime = ();
@MyTime = localtime( time );
$MyDateTime[4]++; #months start at zero, so must add 1
#
# Now the array has the date and time info in the following elements:
# 0 1 2 3 4 5 6 7 8
# (sec, min, hour, mday, mon, year, wday, yday, isdst)
# mday is Day of Month
# year is number of eyars from 1900
# wday is day of week: 0:Sun and 6: Sat
# yday is number of days from 1 Jan
# isdst if true then Daylight savings time is on
So to get your date in the format:
my $aDate = sprintf "%02d%02d%02d",
$MyDateTime[5] % 100, % does a modulo against the year, so 105 comes out as 5, 106 as 6
$MyDateTime[4],
$MyDateTime[4];

$aDate should now have 050303

Wags ;)

QUOTE
get todays date and convert it to yymmdd and assign it to aDate
subsitute in a command such as
$cmd = "integrate -t push -l $aDate"
system($cmd);

I will be running this script on unix. But I would like to be able to
get the date value platform independant.

Thanks.



*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************

Nishi Prafull
On Thu, 3 Mar 2005 12:12:35 -0800, Wagner, David --- Senior Programmer
Analyst --- WGO <[Email Removed]> wrote:
QUOTE
Nishi Prafull wrote:
HI:

I want to write a perl script that would compute the date in the
format yymmdd(050303) and subsitute it for a variable in the perl
script. This variable is thereafter subsituted in a command that will
be run inside the script.

myTest.pl

var aDate;
You can do it a number of ways, here is a start on one way:

my @MyDateTime = ();
@MyTime = localtime( time );
$MyDateTime[4]++;              #months start at zero, so must add 1
#
# Now the array has the date and time info in the following elements:
#    0    1    2    3    4    5    6    7    8
#  (sec, min, hour, mday, mon, year, wday, yday, isdst)
# mday is Day of Month
# year is number of eyars from 1900
# wday is day of week: 0:Sun and 6: Sat
# yday is number of days from 1 Jan
# isdst if true then Daylight savings time is on
So to get your date in the format:
my $aDate = sprintf "%02d%02d%02d",
$MyDateTime[5] % 100,          % does a modulo against the year, so 105 comes out as 5, 106 as 6
$MyDateTime[4],
$MyDateTime[4];

$aDate should now have 050303

Wags ;)
Hi:

Thanks.
I tried the above but it did not return the correct result
my @MyDateTime = ();
@MyTime = localtime(time);
$MyDateTime[4]++;
my $someDate = sprintf
"%02d%02d%02d",$MyDateTime[5]%100,$MyDateTime[4],$MyDateTime[4];
print "$someDaten";

It returned 000101. I need to run this program everyday so that it
takes the system/current date on that day and returns it in the format
yymmdd.

Thanks.



QUOTE

get todays date and convert it to yymmdd and assign it to aDate
subsitute in a command such as
$cmd = "integrate -t push -l $aDate"
system($cmd);

I will be running this script on unix. But I would like to be able to
get the date value platform independant.

Thanks.

*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************



David --- Senior Programmer Anal
Nishi Prafull wrote:
QUOTE
On Thu, 3 Mar 2005 12:12:35 -0800, Wagner, David --- Senior Programmer
Analyst --- WGO <[Email Removed]> wrote:
Nishi Prafull wrote:
HI:

I want to write a perl script that would compute the date in the
format yymmdd(050303) and subsitute it for a variable in the perl
script. This variable is thereafter subsituted in a command that
will be run inside the script.

myTest.pl

var aDate;
You can do it a number of ways, here is a start on one way:

my @MyDateTime = ();
@MyTime = localtime( time );
$MyDateTime[4]++;              #months start at zero, so must add 1
# # Now the array has the date and time info in the following
elements: #    0    1    2    3    4    5    6    7    8
#  (sec, min, hour, mday, mon, year, wday, yday, isdst) # mday is
Day of Month # year is number of eyars from 1900
# wday is day of week: 0:Sun and 6: Sat
# yday is number of days from 1 Jan
# isdst if true then Daylight savings time is on
So to get your date in the format:
my $aDate = sprintf "%02d%02d%02d",
$MyDateTime[5] % 100,
% does a modulo against the
year, so 105 comes out as 5,
106 as 6 $MyDateTime[4], $MyDateTime[4];

$aDate should now have 050303

Wags ;)
Hi:
Thanks.
I tried the above but it did not return the correct result
my @MyDateTime = ();
@MyTime = localtime(time);
Missed this and it should be @MyDateTime and not @MyTime


Also as part of your code, you should always use:

use strict;
use warnings;

By doing this it will cut down on the number of problems you run into.
I made the program as :

#!perl

use strict;
use warnings;

my @MyDateTime = ();
@MyDateTime = localtime(time);
$MyDateTime[4]++;
my $someDate = sprintf "%02d%02d%02d",
$MyDateTime[5]%100,
$MyDateTime[4],$MyDateTime[4];
print "$someDaten";

and it printed out:
050303

Wags ;)


QUOTE
$MyDateTime[4]++;
my $someDate = sprintf
"%02d%02d%02d",$MyDateTime[5]%100,$MyDateTime[4],$MyDateTime[4];
print "$someDaten";

It returned 000101. I need to run this program everyday so that it
takes the system/current date on that day and returns it in the format
yymmdd.

Thanks.




get todays date and convert it to yymmdd and assign it to aDate
subsitute in a command such as
$cmd = "integrate -t push -l $aDate"
system($cmd);

I will be running this script on unix. But I would like to be able
to get the date value platform independant.

Thanks.

*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************


Jose Nyimi
QUOTE
-----Message d'origine-----
De: Wagner, David --- Senior Programmer Analyst --- WGO
[mailto:[Email Removed]]
Envoy: jeudi 3 mars 2005 21:40
: Nishi Prafull
Cc: [Email Removed]
Objet: RE: Perl program to convert system date to yymmdd

Nishi Prafull wrote:
On Thu, 3 Mar 2005 12:12:35 -0800, Wagner, David --- Senior
Programmer
Analyst --- WGO <[Email Removed]> wrote:
Nishi Prafull wrote:
HI:

I want to write a perl script that would compute the date in the
format yymmdd(050303) and subsitute it for a variable in the perl
script. This variable is thereafter subsituted in a command that
will be run inside the script.

myTest.pl

var aDate;
You can do it a number of ways, here is a start on one way:

my @MyDateTime = ();
@MyTime = localtime( time );
$MyDateTime[4]++;              #months start at zero, so must add
1
# # Now the array has the date and time info in the following
elements: #    0    1    2    3    4    5    6    7    8
#  (sec, min, hour, mday, mon, year, wday, yday, isdst) # mday is
Day of Month # year is number of eyars from 1900
# wday is day of week: 0:Sun and 6: Sat
# yday is number of days from 1 Jan
# isdst if true then Daylight savings time is on
So to get your date in the format:
my $aDate = sprintf "%02d%02d%02d",
$MyDateTime[5] % 100,
% does a modulo against the
year, so 105 comes out as 5,
106 as 6 $MyDateTime[4], $MyDateTime[4];

$aDate should now have 050303

Wags ;)
Hi:
Thanks.
I tried the above but it did not return the correct result
my @MyDateTime = ();
@MyTime = localtime(time);
Missed this and it should be @MyDateTime and not @MyTime

Also as part of your code, you should always use:

use strict;
use warnings;

By doing this it will cut down on the number of problems you run
into.
I made the program as :

#!perl

use strict;
use warnings;

my @MyDateTime = ();
@MyDateTime = localtime(time);
$MyDateTime[4]++;
my $someDate = sprintf "%02d%02d%02d",
$MyDateTime[5]%100,
$MyDateTime[4],$MyDateTime[4];

Typo ?
You twice $MyDateTime[4]
Souldn't be: $MyDateTime[4],$MyDateTime[3]; ?

You just lucky that today the month and day have same value 03 :-)

Regards,
Jos.


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.