Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

Replies are listed 'Best First'.
Re: How to get the current Date and Time using Perl on windows?
by GrandFather (Sage) 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 Punitha (Priest) on Jan 02, 2008 at 07:20 UTC


    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


      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.



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

    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:

      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://659915]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2017-11-21 01:35 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (294 votes). Check out past polls.