469,292 Members | 1,312 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

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

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
3 7749
numberwhun
3,503 Expert Mod 2GB
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
4,059 Expert 2GB
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
Rocko
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.

Similar topics

1 post views Thread by Johnson Bruno | last post: by
10 posts views Thread by Newsscanner | last post: by
stormrider
4 posts views Thread by stormrider | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.