How to convert date from MM / YY / DD to Mon Day, Year in Perl?

I need to convert a date to a string. The date is entered as 07/04/2010

and then should be read July 4th 2010

. It must also be entered using single digits instead of double digits (7 instead of 07, and it needs to add 20 to the year if the user only enters / 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.

      

+2


a source to share


4 answers


Don't reinvent the wheel if you already have round wheels. :-) Use a module like DateTime or Time :: Piece .

For instance...



#!/usr/bin/perl

use strict;
use warnings;

use CGI qw(:standard);
use Time::Piece;

my $date = param('Date');
my $t = Time::Piece->strptime($date, '%m/%d/%Y');
$date = $t->strftime('%B %e, %Y');

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

      

- Michael

+4


a source


This works as expected:

#!/usr/bin/perl
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
while(<DATA>) {
   $date=$_;
   $date =~ /^\s*([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})\s*$/;
   $month = int($1-1);
   $day = $2;
   $year = $3;
   $year="20$year" if ($year=~/^\d\d$/);
   $date = $months[$month]." ".$day.", ".$year;

   print "date=$date\n";
}

__DATA__
7/4/1999    
   07/4/2010
1/1/2011
12/31/2010
4/4/09

      

Output:



date=July 4, 1999
date=July 4, 2010
date=January 1, 2011
date=December 31, 2010
date=April 4, 2009

      

I guess the problem is getting data from the form.

+1


a source


Your Perl code is somewhat quirky (you don't check if your match was a regular expression before using its results), but the first warning you get should give you a problem. You are using an uninitialized value in your regex.

That should tell you it $date

's undefined. This means that your input (form or query parameter) does not have a Date parameter . Try to fix this and then fix any problems in your code mentioned by other people answering you.

+1


a source


You are getting these warning messages because the value $date

doesn't match your regex. Try to print the value $date

:

use CGI qw(:standard); 
use strict; 
use warnings;

#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'); 
print "date >>>$date<<<\n";

      

It's also a good idea to check your regex match before trying to use the resulting values.

#break date apart 
if ($date =~ /([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,2}|20[0-9]{2,2})/) {
    $month = $1; 
    $day = $2; 
    $year = $3; 
}
else {
    die "Error: unexpected date format: $date";
}

      

0


a source







All Articles