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

perl date conversion

Status
Not open for further replies.

anderst

IS-IT--Management
Mar 18, 2009
2
0
0
Hi!

I need help with date conversion, I need a perl-script that transform
Wed Mar 28 00:00:00 GMT+02:00 2007
to
2007-03-28

I have tried to use perl modules Date::Manip and Date::Calc, but no succes so far. Maybe I am doing it the wrong way.


// A
 
#!/usr/bin/perl
my $sysin = 'Wed Mar 28 00:00:00 GMT+02:00 2007';
my $date = ConvertDate($sysin);
print $date;
#End Main----------------------------------------------------------------
sub ConvertDate {

my %month = (
'Jan' => '01',
'feb' => '02',
'Mar' => '03',
'Apr' => '04',
'May' => '05',
'Jun' => '06',
'Jul' => '07',
'Aug' => '08',
'Sep' => '09',
'Oct' => '10',
'Nov' => '11',
'Dec' => '12',
);
my (@field) = split(' ',$sysin);
return("$field[-1]-$month{$field[1]}-$field[2]");
}
 
If you have a string always with that format you can do
Code:
my$months='JanFebMarAprMayJunJulAugSepOctNovDec';
my$datestring='Wed Mar 28 00:00:00 GMT+02:00 2007';
my@parts=split/\s/,$datestring;
my$resultstring=sprintf("%4d-%02d-%02d",$parts[$#parts],index($months,$parts[1])/3+1,$parts[2]);

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top