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;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.