perl - converting a date into a string

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-04-25T23:27:01Z Indexed on 2010/04/25 23:33 UTC
Read the original article Hit count: 243

Filed under:

I need to convert a date to a string, the date is entered as 07/04/2010 and should then read July 4th 2010. It should also be able to be entered using singe digits instead of double (7 instead of 07, and it needs to add the 20 to the year if the user enters only /10)

This is what I have so far -

#!/usr/bin/perl
use CGI qw(:standard);
use strict;

#declare variables
my ($date, $month, $day, $year);
my @months = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 

#assign input item to variable
$date = param('Date');

#break date apart
$date =~ /([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,2}|20[0-9]{2,2})/;
$month = $1;
$day = $2;
$year = $3;
unless($year =~ /20[0-9]{2,2}/){
    $year = "20".$year;
}
$date = $months[int($1)]." ".$day.", ".$year;

#display date
print "<HTML><HEAD><TITLE>The Date</TITLE></HEAD>\n";
print "<BODY>\n";
print "The date is: $date\n";
print "</BODY></HTML>\n";

However I keep getting errors Use of uninitialized value in pattern match (m//) at c08ex6.cgi line 14. Use of uninitialized value in pattern match (m//) at c08ex6.cgi line 18. Use of uninitialized value in concatenation (.) or string at c08ex6.cgi line 19. Use of uninitialized value in int at c08ex6.cgi line 21. Use of uninitialized value in concatenation (.) or string at c08ex6.cgi line 21.

© Stack Overflow or respective owner

Related posts about perl