vroom has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

How do I find today's date?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I find today's date?
by vroom (His Eminence) on Jan 19, 2000 at 00:33 UTC
    use localtime or Time::localtime
    my($day, $month, $year)=(localtime)[3,4,5]; print "$day-".($month+1)."-".($year+1900)."\n"; # or use Time::localtime; $tm=localtime; my ($day,$month,$year)=($tm->mday,$tm->month,$tm->year);
    Edited to fix precedence errors in localtime() version as noted by 3dbc
Re: How do I find today's date?
by ducky (Scribe) on Mar 22, 2000 at 23:54 UTC
    If you just need a timestamp string, ala "Wed Mar 22 10:56:43 PST 2000" you can just: print scalar localtime() ; -Ducky

      It sure would be nice to expand this to include how to find some other simple dates:

      • Yesterday
      • tomorrow
      • same time last week
      • same time next week
      • same day next month
      • same day last month
      • same day last year
      • same day next year.

      Skip

        The core modules Time::Piece and Time::Seconds make this very easy.

        #!/usr/bin/env perl use strict; use warnings; use Time::Piece; use Time::Seconds; my $now = localtime; print "Today is $now\n"; my $tp = $now - ONE_DAY; print "Yesterday was $tp\n"; $tp = $now + ONE_DAY; print "Tomorrow will be $tp\n"; $tp = $now - 7 * ONE_DAY; print "A week ago was $tp\n"; $tp = $now + 7 * ONE_DAY; print "Next week will be $tp\n"; $tp = $now->add_months (-1); print "A month ago was $tp\n"; $tp = $now->add_months (1); print "Next month will be $tp\n"; $tp = $now->add_years (-1); print "A year ago was $tp\n"; $tp = $now->add_years (1); print "Next year will be $tp\n";

        🦛

Re: How do I find today's date?
by Melkurion (Initiate) on Sep 03, 2001 at 15:20 UTC
    Here's a complete little program:
    use Time::localtime; my $tm = localtime; printf "The current date is %04d-%02d-%02d\n", $tm->year+1900, ($tm->m +on)+1, $tm->mday; printf "The current time is %02d:%02d:%02d\n", $tm->hour, $tm->min, $t +m->sec;
Re: How do I find today's date?
by autarch (Hermit) on May 10, 2004 at 19:37 UTC
    use DateTime; my $dt = DateTime->today; print $dt->date;
Re: How do I find today's date?
by Agyeya (Hermit) on May 10, 2004 at 05:58 UTC
    You could also use Date::Manip
    $date = ParseDate("today"); print "Today's Date is $date.\n";
      If you want to make the date more human-readable, you can use Date::Manip::UnixDate to format it:
      $date = UnixDate( ParseDate("today") => '%b %e, %Y' ); print "Today's Date is $date.\n"; # MMM DD, YYYY (day is space-padded)

      --sacked
Re: How do I find today's date?
by sacked (Hermit) on May 11, 2004 at 13:58 UTC
    You can also use POSIX::strftime:
    use POSIX qw(strftime); print "today's date is ", strftime( '%b %e, %Y', localtime );
Re: How do I find today's date?
by DeadPoet (Scribe) on Jan 08, 2011 at 18:07 UTC

    Example 1:

    sub timestamp { return localtime (time); } print '[' . timestamp() . ']: Normal Time Format'. "\n";

    Output 1:

    [Sat Jan 8 11:59:07 2011]: Normal Time Format

    Example 2:

    use Time::localtime; sub timestamp { my $t = localtime; return sprintf( "%04d-%02d-%02d_%02d:%02d:%02d", $t->year + 1900, $t->mon + 1, $t->mday, $t->hour, $t->min, $t->sec ); } print '[' . timestamp() . ']: Custom Time Format'. "\n";

    Output 2:

    [2011-01-08_12:06:05]: Custom Time Format
Re: How do I find today's date?
by 3dbc (Monk) on Feb 05, 2004 at 22:39 UTC
    String found where operator expected at date.pl line 1, near "1."-"" (Missing operator before "-"?) String found where operator expected at date.pl line 1, near "1900."\ n"" (Missing operator before "\n"?) syntax error at date.pl line 11, near "1."-"" print "$day\-".($month+1)."-".($year+1900)."\n";
      This is great example of how not to get people to help you. No info about what you're doing with this, and no script.

      The line of code you did send works for me:

      #!perl-w $day=22; $month=7; $year=104; print "$day\-".($month+1)."-".($year+1900)."\n";

      Without the rest of the script, it's impossible to tell where it's breaking exactly, though my guess is the variables don't contain what you think.

      But I'd like to take the opportunity to suggest a couple of optimisations:

      Using a '.' to join strings in a print() statement is expensive - you are doing 2 operations when 1 is enough. print() expects a comma separated list, so:

      print $day,"\-",($month+1),"-",($year+1900),"\n"; # less expensive

      looks the same on output but behind the scenes is less expensive to use, just how much less expensive?

      #! perl-w $day=22; $month=7; $year=104; $count=-5; use Benchmark qw(:all) ; $results = timethese($count, { 'Concatinated' => sub { print $day."\-".($month+1)."-" +.($year+1900)."\n";}, 'Commafied' => sub { print $day,"\-",($month+1),"-",($ +year+1900),"\n"; }, }, 'none' ); cmpthese( $results ) ;

      running the above code I got this

      Rate Commafied Concatinated Commafied 74894/s -- -71% Concatinated 260000/s 247% --

      that's means print()ing with commas is over 300% faster.

      Using quotes (") on a variable with a print() statement is likewise usually a bad idea because perl has to convert the same information to a string twice, once because of the quotes, the second time to print it.

      print $day."\-".($month+1)."-".($year+1900)."\n"; # less expensive

      Benchmarking this is less dramatic than the concatination Vs commas test - it shows an improvement of 6%. However if you're looking to speed up an script these are the kinds of things you should check for first.

      You spend twenty years learning the spell that makes nude virgins appear in your bedroom, and then you're so poisoned by quicksilver fumes and half-blind from reading old grimoires that you can't remember what happens next.

        This looks like premature optimization to me. Write the code for maximum readability in the first instance. If it's too slow, benchmark then apply focused and documented changes to improve speed at bottlenecks.

        print implies I/O which almost always implies unavoidable overhead that makes quite a lot of related "code inefficiency" irrelevant.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Add space before and after the '.'
      print "$day\-" . ($month+1) . "-" . ($year+1900) . "\n";
Re: How do I find today's date?
by Anonymous Monk on Aug 13, 2004 at 15:49 UTC
    poop

    Originally posted as a Categorized Answer.