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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rocko
    New Member
    • Dec 2007
    • 2

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

    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:

    [code=perl]
    #! usr/bin/perl -w

    $logfilename = "logfileLOV ";
    $dateAndtime = `date`;
    $dateAndtime =~ tr// /;
    $logfilename = $logfilename. "_" .$dateAndtime;
    print "$logfilename\n ";
    [/code]

    I have tried running this on command prompt but its not working. Can someone please assist me in this

    Thanks
    Rocko
    Last edited by numberwhun; Jan 2 '08, 10:13 AM. Reason: add code tags
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by Rocko
    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:

    [code=perl]
    #! usr/bin/perl -w

    $logfilename = "logfileLOV ";
    $dateAndtime = `date`;
    $dateAndtime =~ tr// /;
    $logfilename = $logfilename. "_" .$dateAndtime;
    print "$logfilename\n ";
    [/code]

    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:

    [code=perl]
    ############### ############### ############### ############### ##############
    # Date information for time stamps
    ############### ############### ############### ############### ##############
    my $dtstamp;
    my $datetime;
    my @months = qw(01 02 03 04 05 06 07 08 09 10 11 12);
    my @weekdays = qw(01 02 03 04 05 06 07);
    (my $second, my $minute, my $hour, my $dayofmonth, my $month, my $yearoffset, my $dayofweek, my $dayofyear, my $daylightsaving s) = localtime();
    my $year = 1900 + $yearoffset;
    my $thetime = "$hour:$minute: $second, $weekdays[$dayofweek] $months[$month] $dayofmonth, $year";

    if ( $dayofmonth > 9 )
    {
    my $dt = $months[$month] . "/" . $dayofmonth . "/" . $year;
    $dtstamp = $year . $months[$month] . $dayofmonth;
    $datetime = $year . $months[$month] . $dayofmonth . "." . $hour . $minute . $second;
    }
    else
    {
    my $dt = $months[$month] . "/" . '0'.$dayofmonth . "/" . $year;
    $dtstamp = $year . $months[$month] . '0'.$dayofmonth ;
    $datetime = $year . $months[$month] . '0'.$dayofmonth . "." . $hour . $minute . $second;
    }
    [/code]

    That is what I typically use for date/time stamps.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      something as simple as this might work:

      Code:
      my $filename = 'foo';
      my $date = scalar(localtime);
      $date =~ s/\s+/_/g;
      print "filename_$date";
      You can get as complicated as you want creating time/date stamps but generally keeping them in raw format is best:

      Code:
      $date = time;
      print $date;
      POSIX is good for formatting dates too. Or write your own routine like Jeff has done.

      Comment

      • Rocko
        New Member
        • Dec 2007
        • 2

        #4
        Thanks for your assistance guys.

        Cheers!!
        Rocko

        Comment

        Working...