Quote:
Originally Posted by Rocko
Hi,
Wishing you a very Happy New Year!! I am new to perl and I need to know that how to get the current date & time in my perl script. I am working on Windows XP. my code snippet is like this:
-
#! usr/bin/perl -w
-
-
$logfilename = "logfileLOV";
-
$dateAndtime = `date`;
-
$dateAndtime =~ tr// /;
-
$logfilename = $logfilename. "_" .$dateAndtime;
-
print "$logfilename\n";
-
I have tried running this on command prompt but its not working. Can someone please assist me in this
Thanks
Rocko
If you run the "date" command in dos, you will find that it not only returns what is in the system presently as the date, but also prompts for you to make changes.
It is more efficient for you to use the localtime() function in Perl to achieve this, as so:
-
##########################################################################
-
# Date information for time stamps
-
##########################################################################
-
my $dtstamp;
-
my $datetime;
-
my @months = qw(01 02 03 04 05 06 07 08 09 10 11 12);
-
my @weekdays = qw(01 02 03 04 05 06 07);
-
(my $second, my $minute, my $hour, my $dayofmonth, my $month, my $yearoffset, my $dayofweek, my $dayofyear, my $daylightsavings) = localtime();
-
my $year = 1900 + $yearoffset;
-
my $thetime = "$hour:$minute:$second, $weekdays[$dayofweek] $months[$month] $dayofmonth, $year";
-
-
if ( $dayofmonth > 9 )
-
{
-
my $dt = $months[$month] . "/" . $dayofmonth . "/" . $year;
-
$dtstamp = $year . $months[$month] . $dayofmonth;
-
$datetime = $year . $months[$month] . $dayofmonth . "." . $hour . $minute . $second;
-
}
-
else
-
{
-
my $dt = $months[$month] . "/" . '0'.$dayofmonth . "/" . $year;
-
$dtstamp = $year . $months[$month] . '0'.$dayofmonth;
-
$datetime = $year . $months[$month] . '0'.$dayofmonth . "." . $hour . $minute . $second;
-
}
-
That is what I typically use for date/time stamps.
Regards,
Jeff