Code to put today's date into MM-DD-YY format

R

Ron

I am new to cgi I would like to place a check box on my form. When it is
checked & I run my custom Upload cgi script I would like this code to.

1. Get the current date & day of the week.
2. Put it into this format MM-DD-YY Day

I have no ideal where to start. Suggestions Please?

Thanks,
Ron
 
G

Gunnar Hjalmarsson

Ron said:
I am new to cgi I would like to place a check box on my form. When
it is checked & I run my custom Upload cgi script I would like this
code to.

1. Get the current date & day of the week.
2. Put it into this format MM-DD-YY Day

I have no ideal where to start. Suggestions Please?

http://learn.perl.org/
 
M

Mothra

Hi Ron,

Ron said:
I am new to cgi I would like to place a check box on my form. When it is
checked & I run my custom Upload cgi script I would like this code to.

1. Get the current date & day of the week.
2. Put it into this format MM-DD-YY Day

I have no ideal where to start. Suggestions Please?

Thanks,
Ron

use strict;
use warnings;
use DateTime;

my $dt = DateTime->today;

my $date = $dt->mdy;
my $dname = $dt->day_name;

print $date . $dname ."\n";

Mothra
 
J

Julian Mehnle

Ron said:
1. Get the current date & day of the week.
2. Put it into this format MM-DD-YY Day

On an off-topic note: you should really use the internationally
standardized ISO date format[1], which is YYYY-MM-DD. Most other date
formats, like MM-DD-YY, are either confusing or ambiguous, or both.

Even if you can't use the ISO format in your application immediately, you
should plan using it in the future and advocate it towards friends,
colleagues, and bosses. ;-)

[1] http://www.cl.cam.ac.uk/~mgk25/iso-time.html
 
M

Matija Papec

X-Ftn-To: Ron

Ron said:
I am new to cgi I would like to place a check box on my form. When it is
checked & I run my custom Upload cgi script I would like this code to.

1. Get the current date & day of the week.
2. Put it into this format MM-DD-YY Day

I have no ideal where to start. Suggestions Please?

my($dd,$mm,$yy,$day) = (localtime)[3,4,5,6];
my $today = join '-', map sprintf("%02d", $_), ($mm+1,$dd,$yy%100);
$day = (qw/Sun Mon Tue Wed Thu Fri Sat/)[$day];
print "$today $day\n";
 
E

Eric J. Roode

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

I am new to cgi I would like to place a check box on my form. When it is
checked & I run my custom Upload cgi script I would like this code to.

1. Get the current date & day of the week.
2. Put it into this format MM-DD-YY Day

I have no ideal where to start. Suggestions Please?

Use my new Time::Format module, available on CPAN and at ActiveState's
repository:

use Time::Format;
$string = $time{'mm-dd-yy Day'};

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

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

iQA/AwUBPxlCFGPeouIeTNHoEQKenwCfY3y57SQxb4qHYSlWEZu6IVyG5g0An3aV
Ls/K8ETz9fYfMQraNX7DJxmr
=CF5O
-----END PGP SIGNATURE-----
 
D

Devdas Bhagat

Thanks Matija,

That works great. Just one more little problem (My Fault) I need the year
in YYYY format?

How would I change this to get MM-DD-YYYY Day?

my($dd,$mm,$yy,$day) = (localtime)[3,4,5,6];
my $today = join '-', map sprintf("%02d", $_), ($mm+1,$dd,$yy%100);
my $today = join '-', map sprintf("%02d", $_), ($mm+1, $dd, $yy+1900);

I would recommend
my $today = sprintf("%02d-%02d-%d", $mm+1, $dd, $yy+1900);
Much easier to follow and it should be faster.

Devdas Bhagat
 
T

Tony Curtis

On 20 Jul 2003 15:57:21 GMT,
Devdas Bhagat <[email protected]> said:
Thanks Matija,

That works great. Just one more little problem (My
Fault) I need the year in YYYY format?

How would I change this to get MM-DD-YYYY Day?

my($dd,$mm,$yy,$day) = (localtime)[3,4,5,6]; my $today
= join '-', map sprintf("%02d", $_),
($mm+1,$dd,$yy%100);
my $today = join '-', map sprintf("%02d", $_), ($mm+1,
$dd, $yy+1900);
I would recommend my $today = sprintf("%02d-%02d-%d",
$mm+1, $dd, $yy+1900); Much easier to follow and it
should be faster.

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

Let the module take the strain.
 
E

Eric J. Roode

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

That works great. Just one more little problem (My Fault) I need the
year in YYYY format?

How would I change this to get MM-DD-YYYY Day?

use Time::Format;
my $date = $time{'mm-dd-yyyy Day'};
print $date;

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

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

iQA/AwUBPxsqkmPeouIeTNHoEQJ1HQCfaODx8mA5pYTwwyvLpPcWUdmeZvMAoNMM
SbY2cqGWeSVdvMa3EcEAzu65
=ZFBS
-----END PGP SIGNATURE-----
 
E

Eric J. Roode

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

....
I would recommend
my $today = sprintf("%02d-%02d-%d", $mm+1, $dd, $yy+1900);
Much easier to follow and it should be faster.

Don't forget that he wanted the "Day" in there too.

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

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

iQA/AwUBPxsqzmPeouIeTNHoEQJ1UgCgnBebWzZ7U76D9QtvsKgdV87NwwQAoLak
4CcEJUHUhbgJHrNo7fag7ZO5
=8jxz
-----END PGP SIGNATURE-----
 
B

Bob Walton

Ron wrote:

....

1. Get the current date & day of the week.
2. Put it into this format MM-DD-YY Day

I have no ideal where to start. Suggestions Please? ....


Ron


You can't get much easier than:


use Date::Manip;
$ENV{TZ}='EDT'; #only if your OS doesn't define it
print UnixDate('today','%m-%d-%y %A');

or

print UnixDate('today','%m-%d-%Y %A);

for your subsequent request.
 
G

Gunnar Hjalmarsson

Julian said:
On an off-topic note: you should really use the internationally
standardized ISO date format[1], which is YYYY-MM-DD. Most other
date formats, like MM-DD-YY, are either confusing or ambiguous, or
both.

Even if you can't use the ISO format in your application
immediately, you should plan using it in the future and advocate it
towards friends, colleagues, and bosses. ;-)

[1] http://www.cl.cam.ac.uk/~mgk25/iso-time.html

That comment made me reconsider the way I have been dealing with dates
in an internationalized program I'm maintaining. I concluded that
there is no need to struggle with 'Jan' or 'January' etc., creating a
need to translate the months.

I was able to reduce my need to three types of time representations:
1) A pure time stamp
2) Today's date
3) The date X days ago

So I wrote this code:

our $time = time;

sub timestamp {
my $fmt = (shift or '');
my $days = (shift or 0);
my @t = (gmtime($time - $days * 86400))[0..5];
return (sprintf "%d-%02d-%02d", $t[5] += 1900, ++$t[4], $t[3])
. ($fmt eq 'date' ? '' : sprintf "T%02d-%02d-%02dZ", @t[2,1,0]);
}

timestamp() returns 2003-07-22T20-28-04Z
timestamp('date') returns 2003-07-22
timestamp('date', 5) returns 2003-07-17

That's all. Thanks for making me think, Julian. :)
 
G

Gunnar Hjalmarsson

Correction!!

Gunnar said:
sub timestamp {
my $fmt = (shift or '');
my $days = (shift or 0);
my @t = (gmtime($time - $days * 86400))[0..5];
return (sprintf "%d-%02d-%02d", $t[5] += 1900, ++$t[4], $t[3])
. ($fmt eq 'date' ? '' : sprintf "T%02d-%02d-%02dZ", @t[2,1,0]); ---------------------------------------------^----^
}

timestamp() returns 2003-07-22T20-28-04Z
------------------------------------------------^--^

Should of course be colons.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top