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
irene79Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GnissmanCommented:
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
0
irene79Author Commented:
is there any ways that i can check whether the final string is a valid date?
0
GnissmanCommented:
Only way I know of is handing the values over to a call of timelocal() and see if it returns an error. But this function only checks if the day is less than 32, no matter what month is specified. So 31.11.1999 will result in on error...

Of course you can always write your own function to check it.

Gniss
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

prakashk021799Commented:
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
0
irene79Author Commented:
this is a module rite? meaning i have to get the administrator to run this file or wat? is there anyway other than module?
0
irene79Author Commented:
Thanks! got the answer from ozo on the validation on date!
0
GnissmanCommented:
Could you perhaps post ozo's answer? Just to make this thread complete...

Gniss
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.