Connecting Tech Pros Worldwide Forums | Help | Site Map

How to get the current Date and Time using Perl on windows

Newbie
 
Join Date: Dec 2007
Posts: 2
#1: Jan 2 '08
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:

Expand|Select|Wrap|Line Numbers
  1. #! usr/bin/perl -w
  2.  
  3. $logfilename = "logfileLOV";
  4. $dateAndtime = `date`;
  5. $dateAndtime =~ tr// /;
  6. $logfilename = $logfilename. "_" .$dateAndtime;
  7. print "$logfilename\n";
  8.  
I have tried running this on command prompt but its not working. Can someone please assist me in this

Thanks
Rocko

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,573
#2: Jan 2 '08

re: How to get the current Date and Time using Perl on windows


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:

Expand|Select|Wrap|Line Numbers
  1. #! usr/bin/perl -w
  2.  
  3. $logfilename = "logfileLOV";
  4. $dateAndtime = `date`;
  5. $dateAndtime =~ tr// /;
  6. $logfilename = $logfilename. "_" .$dateAndtime;
  7. print "$logfilename\n";
  8.  
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:

Expand|Select|Wrap|Line Numbers
  1. ##########################################################################
  2. #  Date information for time stamps
  3. ##########################################################################
  4. my $dtstamp;
  5. my $datetime;
  6. my @months = qw(01 02 03 04 05 06 07 08 09 10 11 12);
  7. my @weekdays = qw(01 02 03 04 05 06 07);
  8. (my $second, my $minute, my $hour, my $dayofmonth, my $month, my $yearoffset, my $dayofweek, my $dayofyear, my $daylightsavings) = localtime();
  9. my $year = 1900 + $yearoffset;
  10. my $thetime = "$hour:$minute:$second, $weekdays[$dayofweek] $months[$month] $dayofmonth, $year";
  11.  
  12. if ( $dayofmonth > 9 )
  13.    my $dt = $months[$month] . "/" . $dayofmonth . "/" . $year; 
  14.    $dtstamp = $year . $months[$month] . $dayofmonth;
  15.    $datetime = $year . $months[$month] . $dayofmonth . "." . $hour . $minute . $second;
  16. }
  17. else
  18. {
  19.    my $dt = $months[$month] . "/" . '0'.$dayofmonth . "/" . $year; 
  20.    $dtstamp = $year . $months[$month] . '0'.$dayofmonth;
  21.    $datetime = $year . $months[$month] . '0'.$dayofmonth . "." . $hour . $minute . $second;
  22. }
  23.  
That is what I typically use for date/time stamps.

Regards,

Jeff
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Jan 2 '08

re: How to get the current Date and Time using Perl on windows


something as simple as this might work:

Expand|Select|Wrap|Line Numbers
  1. my $filename = 'foo';
  2. my $date = scalar(localtime);
  3. $date =~ s/\s+/_/g;
  4. print "filename_$date";
You can get as complicated as you want creating time/date stamps but generally keeping them in raw format is best:

Expand|Select|Wrap|Line Numbers
  1. $date = time;
  2. print $date;
POSIX is good for formatting dates too. Or write your own routine like Jeff has done.
Newbie
 
Join Date: Dec 2007
Posts: 2
#4: Jan 3 '08

re: How to get the current Date and Time using Perl on windows


Thanks for your assistance guys.

Cheers!!
Rocko
Reply


Similar Perl bytes