Link to home
Start Free TrialLog in
Avatar of irene79
irene79

asked on

Convert string's 1st 8 characters to date and check whether it's a valid date

my variable is "19991130.htm" it can be "19991130.html" sometimes.

so how do i get the 1st 8 characters and covert it to "30/11/1999"?

another question is how do i make sure that the date converted is a valid date?

ozo provided me with the answer
$string =~ s"(\d{4})(\d\d)(\d\d)\.htm"$3/$2/$1";

but the if my filename is .html .. the $string will still be 19991130.htm
Avatar of Gnissman
Gnissman

Let's assume your filename is stored in $file

$string = substr($file, 6, 2)."/".substr($file, 4, 2)."/".substr($file, 0, 4);

This should do it!
Gniss
Avatar of irene79

ASKER

is there any ways that i can check whether the final string is a valid date?
ASKER CERTIFIED SOLUTION
Avatar of Gnissman
Gnissman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use Date::Manip;

$file = '19991130.htm';  # or '19991130.html'

($date, $suffix) = split ('.', $file);

$new_date = UnixDate($date, '%d/%m/%Y');

if ($new_date) {
   print "Date = $new_date\n";
} else {
   print "$date is not valid date.\n";
}

## you can get Date::Manip at any CPAN archive
Avatar of irene79

ASKER

this is a module rite? meaning i have to get the administrator to run this file or wat? is there anyway other than module?
Avatar of irene79

ASKER

Thanks! got the answer from ozo on the validation on date!
Could you perhaps post ozo's answer? Just to make this thread complete...

Gniss