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
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
but the if my filename is .html .. the $string will still be 19991130.htm
ASKER
is there any ways that i can check whether the final string is a valid date?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
$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
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?
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
Gniss
$string = substr($file, 6, 2)."/".substr($file, 4, 2)."/".substr($file, 0, 4);
This should do it!
Gniss