By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Jan 2 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jan 2 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
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.
Jan 2 '08 #3

P: 2
Thanks for your assistance guys.

Cheers!!
Rocko
Jan 3 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.