Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Current Date with Perl

Status
Not open for further replies.

campbere

Technical User
Oct 10, 2000
146
US
Ok I am sure there is probably a function out there for this but I haven't found it. How do I get today's current date using Perl? I would like to get it in two different forms: MMDDYYYY and DD-MON-YY

All I have found so far are refrences to a module called Date:Calc but that seems to be for determing the difference between two dates.

Thank you for the help.
 
localtime is the function I use. I had all kinds of headaches with Date::Calc so never used it.
Put this in your parsing routine and the vars are always available at run time.

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$sec=sprintf("%02d",$sec);
$min=sprintf("%02d",$min);
$hour=sprintf("%02d",$hour);
$mday=sprintf("%02d", $mday);
$mon=sprintf("%02d", $mon+1);
$year=sprintf("%04d", $year+1900);
Keith
 
if you are on a unix box, you can issue a system call something like

$date = `date + "%M%M%D%D%Y%Y"`;

however, check the man page for date to see the options for formatting.
 
You can use the Date::Format module:

use Date::Format;
$date1 = time2str("%m%d%Y", time);
$date2 = time2str("$d-%b-%y", time);

Robin
 
Date::Calc will do the job, honest! Just use the Today() function:

($year,$month,$day) = Today([$gmt]);

... and then parse the results.

Alternately, you can use the "Swiss Army Knife" of date manipulation, Date::Manip. Here's a little snippet that gets the current date and prints it out in the two formats you requested:

#!/usr/bin/perl -w
use strict;

use Date::Manip;

my $now;
$now = localtime(time);
print &UnixDate($now, "%m%d%Y"), "\n";
print &UnixDate($now, "%d-%b-%y"), "\n\n";

****

Output is:

11052002
05-Nov-02

*****

Cheers,
--Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top