Hope this helps, just pass the subroutine a 1 to have it return month/day/year ex: 02/22/2005 or pass it nothing to return $Year$RealMonth${Day}_$Hour${Minute} ex:20050222_122. You can of course play around with the output to return diff formats
sub getdate{
my $ret = "$_[0]";
# Get the all the values for current time
my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOf
+Year, $IsDST) = localtime(time);
# In Perl, you'll need to increment the month by 1
my $RealMonth = $Month + 1; # Months of the year are not zero-base
+d
# Perl code will need to be adjusted for one-digit months
if($RealMonth < 10)
{
$RealMonth = "0" . $RealMonth; # add a leading zero to one-digi
+t months
}
# How to add a leading zero in Perl
if($Day < 10)
{
$Day = "0" . $Day; # add a leading zero to one-digit days
}
# How to use modulo arithmetic in Perl
if($Year >= 100) {
$Fixed_Year = ($Year % 100);
}
else
{
$Fixed_Year = $Year;
}
# 1900 is subtracted from the value returned from localtime
# Add it back in to get a 4-digit year
$Year += 1900;
# Format the string the way we want
if ($ret == "1"){
$today = "$RealMonth\/$Day\/$Year";
return $today;
}
else{
$today = "$Year$RealMonth${Day}_$Hour${Minute}";
return $today;
}
}
|