[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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
0
irene79
Asked:
irene79
  • 3
  • 3
1 Solution
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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