Creating special DATE format

D

\Dandy\ Randy

Hello peoples,

I could use some help writing a script that will create and print the date
in a special format. Here is what I am looking to accomplish:

When my main script is executed:

assign the date to a variable in this format = month/day/year all in two
digit format like so: 09/08/03

something like:

A) if $monthday is less than 10 ... add a "0" before it ... thus creating
01, 02 ,03 etc for the first 9 days of a month, otherwise all other
monthdays can remains in 2 digit format
B) if $month = "January" then $month = "01" and so on for other months
C) if $year = 2003 then remove first two digits, thus creating 03 and so on
for other years
D) Then assemble the date like this: $today = $monthday/$month/$year
E) print $today

I've been search all the doc's I can today but havent been able to find
examples on how to do this. If you can help me, I would be forever greatful.
TIA

Randy
 
G

Gunnar Hjalmarsson

Dandy" Randy said:
When my main script is executed:

assign the date to a variable in this format = month/day/year all
in two digit format like so: 09/08/03

use Date::Format;
my $date = time2str('%D', time);
 
D

\Dandy\ Randy

perldoc POSIX ---> strftime

strftime('%m/%d/%y', ...)

amongst other solutions. If you have the choice, I would
suggest %Y though so that you get the full year format,
and the order %Y-%m-%d (much easier to sort).

Tony, I have done a little reading on this POSIX thingy. The code as you
wrote it above did not seem to function. ... on perdoc, it shows this
example:

$str = POSIX::strftime( "%A, %B %d, %Y", 0, 0, 0, 12, 11, 95, 2 );
print "$str\n";

But what this code does is write the date for Dec 12, 1995 ... i'm looks for
the present date when the code is run in two digit format ... can you help?
TIA
R
 
T

Tony Curtis

"Tony Curtis" wrote
Tony, I have done a little reading on this POSIX
thingy. The code as you wrote it above did not seem to
function. ... on perdoc, it shows this example:

It was simply an outline, indicated by the ellipsis...
$str = POSIX::strftime( "%A, %B %d, %Y", 0, 0, 0, 12,
11, 95, 2 ); print "$str\n";
But what this code does is write the date for Dec 12,
1995 ... i'm looks for the present date when the code is
run in two digit format ... can you help?

Arguments 2... are a list in the order returned by
localtime. So you just want that with localtime's default
argument ("now").

use POSIX qw(strftime);
my $date = strftime('%Y-%d-%m', localtime);

hth
t
 
G

Gunnar Hjalmarsson

Dandy" Randy said:
Gunnar, is the syntax for your code above correct?

Yes, at least it works fine for me. Of course, it presupposes that the
module Date::Format is installed. It's available on my IndigoPerl
installation, which made me believe that it is part of the standard
distribution, but now I realize that it isn't...

Using a module for things like these is convenient. Tony gave you
another suggestion. But of course, it can be done with plain Perl:

my @t = (localtime)[3..5];
my $date = sprintf "%02d/%02d/%02d", ++$t[1], $t[0], $t[2] % 100;
It does not function as written.

What happens?
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello peoples,

I could use some help writing a script that will create and print the
date in a special format. Here is what I am looking to accomplish:

When my main script is executed:

assign the date to a variable in this format = month/day/year all in
two digit format like so: 09/08/03

use Time::Format;
my $date_var = $time{'mm/dd/yy'};

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP1032WPeouIeTNHoEQIr9QCghCzirwQHBLmEHd/zlcjAsOwm9XsAoPVg
5ho/qdFn2pUZiOnKBHV95Do1
=00Ye
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top