473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 8267
numberwhun
3,509 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

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

Similar topics

1
by: Johnson Bruno | last post by:
Hi, I want my perl script to get date and time of another computer which is connected by LAN.The OS is Win 2000. I tried this , but did not work. $file="\\\\P418\\test\\get.cmd";...
10
by: Newsscanner | last post by:
Hello, In my MySQL database, one of the fields eople have to fill in is "DOB" (Date of Birth). I have now managed to begin inserting data into my DB via a form, the data type for the DOB field...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
3
by: Mark Ingram | last post by:
Hi, I'd like to know the best way of checking the current time during a demonstration product. At the minute i store the first run date, then compare that to the system time, but obviously a user...
4
stormrider
by: stormrider | last post by:
Hi all, I'm trying to implement a Perl structure. My small program will do the followings; Take the birth date of the user as input. (Month's will be input as strings.) Take the current time...
12
by: jonathan184 | last post by:
Hi the purpose of the script I am trying to do is to access a dir and run the ls -l command which will show the files and the modified date, no w the script is to check two files in that dir , if the...
2
by: rimvydazas | last post by:
Hi guys, I have a script which deletes necessary entries on specific date. Here is the script: #!/usr/bin/perl -w use DBI; my $dbh = DBI->connect( "dbi:mysql:test", "newbie", "")
6
by: trytobreak | last post by:
Hi All, I am a network administrator in a fairly large software company and I would like to write myself a small utility, which would connect (one by one) to all machines on the network and get...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.