I need to have a script where the date will be entered using numbers and forward slashes. The month and day numbers can be entered using one or two digits, and the year number can be entered using either two or four digits. The date should be displayed, for example, xx/xx/xx as xxxx x, xxxx. I also need the 20 added to the year if it is displayed as xx.

I have started the script but having trouble with the breaking apart the date and adding the 20. I have tried several different ways, but nothing is coming out correctly. Can anyone help me? Many thanks!!!

#!/usr/bin/perl
#date.cgi - converts a numeric date to a string
use CGI qw(:standard -debug);
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


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

There is defiantly a better way to do it...but there always is...but here is my 5 minute solution...I added an empty string for the 0 element of your months array...so that the month numbers match up with the array index (you don't want to end up with the Java Calendar object).

$date =~ s/^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/$months[$1] $2, $3/;
$date =~ s/,\s(\d{2})$/, 20$1/;
print $date;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.