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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date format 3

Status
Not open for further replies.
Feb 16, 2003
87
GB
Hi all.

I need to get the date down to 8 characters eg:

yyyymmdd

so today, 3rd April 2003 would be 20030403

Any ideas what I could use without using any modules?

Simon
 
Without using modules you will have to create a lookup table for the month conversion. The rest is fairly straitforward. The day conversion relies on Perl's 'do the right thing' on string to number conversion. If you run it with warnings on, you will get a complaint. Ignore it.
Code:
 my %month = (  january     => '01',
                february    => '02',
                march       => '03',
                april       => '04',
                may         => '05',
                june        => '06',
                july        => '07',
                august      => '08',
                september   => '09',
                october     => '10',
                november    => '11',
                december    => '12'
            );

my $date = '3rd April 2003';
my ($day, $mon, $year) = split /\s+/, $date;
my $fmtdate = $year . $month{lc $mon} . sprintf '%02d', $day;
jaa
 
That's great jaa - thanks.

How can I get it to use the actual time rather than my $date = '3rd April 2003';?

Simon
 
You can get the actual time with 'localtime' as follows:

($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);
print "$mday:$mon:$year\n";


 
Oooooh! That's great tonykent - perfect little bit of code!

Thank you both gents!

Cheers

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top