• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 548
  • Last Modified:

Extract date from string

I have this string "19991130.htm" how do i make it into "30/11/1999" (dd/mm/yyyy) and insert it into the database whose column is a date field?
0
irene79
Asked:
irene79
  • 2
  • 2
1 Solution
 
ozoCommented:
$string =~ s"(\d{4})(\d\d)(\d\d)\.htm"$3/$2/$1";
0
 
irene79Author Commented:
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?
0
 
ozoCommented:
use Time::Local;
$string =~ s"(\d{4})(\d\d)(\d\d).*"$3/$2/$1";
eval{ ($d,$m,$y)=(localtime timelocal(0,0,0,$3,$2-1,$1-1900))[3,4,5] };
if( $@ || $d!=$3 ){ die $@||"invalid date" }
0
 
irene79Author Commented:
Thanks ozo!!!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now