Re: Date formats
by foogod (Friar) on Jan 09, 2002 at 08:28 UTC
|
#!/usr/bin/perl
use strict;
my($day, $month)=(localtime)[3,4];
$month++;
if ($month < 10) { $month = "0" . $month; }
if ($day < 10) { $day = "0" . $day; }
my $date = "$month/$day";
print $date;
HTH
- f o o g o d | [reply] [d/l] |
|
|
Personally, I've always prefered (s)printf when padding numbers ...
my($d,$m) = (localtime)[3,4];
my $date = sprintf("%02d/%02d",++$m,$d);
--k.
Update: Oops, meant to ++$m not $m++. Thanks to blakem for the catch.
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
Naughty rob_au, bad boy!
You are calling localtime twice, which is setting yourself up for a race condition, should the code ever run on the crack of midnight of new year's eve. You could have the month from one day and the year from another.
Yes it's highly unlikely to happen, but it would be better to
my $date = do {
my @now = localtime;
sprintf("%02d%02d", $now[4] + 1, $now[3]);
};
Why invite trouble?
--g r i n d e r
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
| [reply] [d/l] |
|
|
$date = sprintf("%02d/%02d", 1+(localtime)[4], (localtime)[3]);
#(localtime)[4] runs 0..11 not 1..12
| [reply] [d/l] |
Re: Date formats
by Rich36 (Chaplain) on Jan 09, 2002 at 08:35 UTC
|
Check out the module Date::Calc which allows to do
many, many different things with dates and times.
Rich36
There's more than one way to screw it up...
| [reply] |
|
|
#!/usr/bin/perl
use Date::Manip;
print UnixDate( ParseDate( "today" ), "%m/%d" ), "\n";
-derby | [reply] [d/l] |
Re: Date formats
by Matts (Deacon) on Jan 09, 2002 at 14:50 UTC
|
perl -MTime::Piece -le 'print localtime->strftime("%d/%m")'
Time::Piece makes this stuff easy, and understandable. Of course I'm biased :-) | [reply] [d/l] |
|
|
Because I'm well on my way to becoming a Time::Piece convert, here it is used in a manner similiar to the other entries in this thread:
#!/usr/bin/perl -wT
use strict;
use Time::Piece;
my $t = localtime;
my $date = sprintf("%02d/%02d",$t->mon,$t->mday);
print "$date\n";
-Blake
| [reply] [d/l] |
Re: Date formats
by dmmiller2k (Chaplain) on Jan 09, 2002 at 21:50 UTC
|
Oh well, I suppose I'll add to the cacophany.
use POSIX qw( strftime );
# ...
my $date = strftime("%m/%d", localtime);
dmm
You can give a man a fish and feed him for a day ...
Or, you can teach him to fish and feed him for a lifetime
| [reply] [d/l] |