Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Ethen (Acolyte)
on Jan 02, 2008 at 07:11 UTC ( #659915=perlquestion: print w/ replies, xml ) Need Help??
Ethen has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Wishing you a very Happy New Year!! 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 Ethen . . . . Thanks a lot for your assistance :-) God bless!!! Cheers!! Ethen

Comment on How to get the current Date and Time using Perl on windows?
Download Code
Re: How to get the current Date and Time using Perl on windows?
by Punitha (Priest) on Jan 02, 2008 at 07:20 UTC

    Hi

    You can use the time function in perl as

    my ($sec,$min,$hour,$day,$month,$yr19,@rest) = localtime(time);##### +##To get the localtime of your system print "Date:\t$day-".++$month. "-".($yr19+1900)."\n"; ####To print dat +e format as expected print "Time:\t".sprintf("%02d",$hour).":".sprintf("%02d",$min).":".spr +intf("%02d",$sec)."\n";###To print the current time

    Punitha

      print "Time:\t".sprintf("%02d",$hour).":".sprintf("%02d",$min).":".spr +intf("%02d",$sec)."\n";

      Perhaps you could use printf rather than print and sprintf and avoid concatenation to make that easier to read.

      printf qq{Time:\t%02d:%02d:%02d\n}, $hour, $min, $sec;

      I hope this is of interest.

      Cheers,

      JohnGG

Re: How to get the current Date and Time using Perl on windows?
by GrandFather (Cardinal) on Jan 02, 2008 at 07:29 UTC

    Resorting to invoking system commands is hardly ever the way to get stuff done. Perl has a number of built in commands to retrieve the current time. time gets the current 'epoch' from the system. gmtime or localtime may be used to convert the epoch returned by time to a list of date and time elements.

    However, date and time is a big industry in the Perl world and there are many modules on CPAN that deal with date and time manipulation. The kitchen sink module is Date::Manip. Watch out under Windows though because you will almost certainly need to initialize the time zone before using any of the module's features (see the 'KNOWN PROBLEMS' section).


    Perl is environmentally friendly - it saves trees
Re: How to get the current Date and Time using Perl on windows?
by McDarren (Abbot) on Jan 02, 2008 at 07:33 UTC
    In the windows world, the date command is generally used for setting the date.

    If you want a human-readable date/time stamp, then probably the simplest way is to use localtime.

    For example:

    C:\>perl -le "print scalar localtime" Wed Jan 2 15:27:20 2008

    Cheers,
    Darren :)

      As everyone has pointed out, there are much better, more perlish ways to do it, but here my $0.02 in the interest of TIMTOWTDI in case you're in a DOS batch file mindset.

      First, date /? reports:

      Displays or sets the date. DATE [/T | date] Type DATE without parameters to display the current date setting and a prompt for a new one. Press ENTER to keep the same date. If Command Extensions are enabled the DATE command supports the /T switch which tells the command to just output the current date, without prompting for a new date.

      If Command Extensions are missing, you can pipe the ENTER like this:
      echo.|date

      which is a little harder to parse, but also doesn't require setting the date.

      C:\chas_sandbox>perl -le "print `date /T`" Wed 01/02/2008 C:\chas_sandbox>perl -le "print `echo.\|date`" The current date is: Wed 01/02/2008 Enter the new date: (mm-dd-yy) C:\chas_sandbox>


      I humbly seek wisdom.
        The problem with this is that the OP was after a date and time stamp, and the DOS date command only gives the date. You could I suppose use a combination of both the date and time commands, but that would be just silly™

        Obviously, the OP has a script that was written for a *nix environment and he is trying to port it to winders.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://659915]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-09-21 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls