Gossamer Forum
Home : General : Perl Programming :

Search and translate functions?

Quote Reply
Search and translate functions?
I have a date from the form in format: 2003-04-03, (yyyy-mm-dd).

$date = $field{
'date'} ;

but once after input I want to format it through script as:

Apr-04-03 (Name of Month - day remains same - Year in two digits)

Is it possible?

Thanks,

Zeshan.
Quote Reply
Re: [zeshan] Search and translate functions? In reply to
Try:

require POSIX;
my $date = POSIX::strftime("%a-%m-%Y", localtime);

Last edited by:

Paul: Apr 3, 2003, 3:37 PM
Quote Reply
Re: [Paul] Search and translate functions? In reply to
I don't want the server date or system date 'localtime'.

I specifically want the change of format of date from input. It could be any date.

I appreciate your help.

Thanks,

Zeshan.
Quote Reply
Re: [zeshan] Search and translate functions? In reply to
You'll need to look at some date modules, eg Date::Calc and Date::Convert...

http://search.cpan.org/...te-Calc-5.3/Calc.pod

You'll probably need Date_To_Time() on that page.
Quote Reply
Re: [zeshan] Search and translate functions? In reply to
Try :

#!/perl/bin/perl

use strict;

# The array months with starting zero padding
my @Months = qw(ZERO Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec);

# Test date
my $date = "2003-04-26"; # April 26 2003

my ($YY,$MM,$DD) = split(/-/,$date);
# $YY = 2003
# $MM = 04
# $DD = 26

my $year = substr($YY, 2, 2);
# $year = 03 , assuming year has a length of four

print " Old format : $date ( YYYY-MM-DD )\n";
print " New format : $Months[$MM]-$DD-$year ( Month-DD-YY )\n";

Thanks

cornball
Quote Reply
Re: [cornball] Search and translate functions? In reply to
You can leave out the second 2 from substr(). If you do that it will read to the end of the string, which will only be two characters anyway.
Quote Reply
Re: [Paul] Search and translate functions? In reply to
Hello Paul, Yes you can drop the second 2.

There are many ways to do this.

#!/perl/bin/perl

use strict;

# The array months with starting zero padding
my @Months = qw(ZERO Jan Feb Mar Apr May Jun Jul Aug Sept Nov Dec);

# Test date
my $date = "2003-04-26"; # April 26 2003

if ($date =~ /^\d\d(\d\d)-(\d\d)-(\d\d)$/) {

my $NewFormat = "$Months[$2]-$3-$1";

print " New Format : $NewFormat ( Month-DD-YY )\n";

}

else {

print "Ooops date in not in YYYY-MM-DD format \n";

}

Thanks

Cornball
Quote Reply
Re: [cornball] Search and translate functions? In reply to
I have a similar issue...

I have a standard html form, that users enter date in MM/DD/CCYY format.

I'm trying to get my script to compare that date against today's date, to see whether it is today or older, or new than today.

The only way I can think about is split the MM/DD/CCYY into 3 variables, $formmonth, $formday, $formyear, then compare each against servers date.

I'm trying something like: ($formmonth, $formday, $formyear) = split(///, $fields{'date'};

I cant figure out how to split using "/" as the delimitor. Anyone tried this here?
Quote Reply
Re: [devinci99] Search and translate functions? In reply to
/\//
Post deleted by devinci99 In reply to
Quote Reply
Re: [devinci99] Search and translate functions? In reply to
thx I got it to work...


[s7300:~/ecards_db] stu% ./splitdate.cgi
testing...
06/01/2003
06
01
2003



For variables like $mn and $day, how do I surpress the values to single digits if it's under the value of 10? Because for values under 10, it pads a "0" before it. like if it's 08, i am trying to convert it to just say 8.


o n/m stupid me... $mn = sprintf('%01d', $mn); Unsure

Last edited by:

devinci99: Jun 1, 2003, 3:58 AM