开发者

How do I convert a 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 should then read July 4th 2010. It should a开发者_如何学Pythonlso 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.


Don't reinvent the wheel if perfectly round wheels are already available to you. :-) Use a module such as 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


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 am guessing that the issue is getting the data from the form.


Your Perl code is somewhat dodgy (you don't check if your regular expression match succeeded before using the results from it) but the first warning you are getting should give the problem away. You are using an uninitialized value in your regular expression.

That should tell you that $date is undefined. That implies that your input (either form or query parameter) does not have a parameter called Date in it. Try fixing that and then fix all the problems in your code mentioned by the other people answering you.


You are getting these warning messages because the value of $date does not match your regular expression. Try to print the value of $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 is also a good practice to check if your regex matches before trying to use the captured 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜