Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help With Date Formating ASAP! 1

Status
Not open for further replies.

marms767

ISP
Joined
May 17, 2001
Messages
3
Location
US
I would really be thankful for any help. I'm a ColdFusion coder and do e-commerce for a company. A guy that just quit built a page in Perl that we use in tracking the number of hits on what date by writing them to a text file. The problem is this guy formated his dates to show 2001121. That could be mean January 21st, 2001 or December 1st, 2001!!!

I want it to write the date as 20010121 or 2001-01-21 so I can parse through these text files and count it using an application I already built.

The current Perl code that sets the date is:

($d,$d,$d,$d,$m,$y)=localtime();
$newdate=($y+1900).($m+1).$d;

He then appends $newdate to a text file for each hit.

Could someone please help me with a code that can will change $newdate to yyyy-mm-dd or yyyymmdd?

Thanks so much.
 
Try this:

($d,$d,$d,$d,$m,$y)=localtime();
$m++;
$m=&quot;0&quot; . $m if ($m < 10);
$d=&quot;0&quot; . $d if ($d < 10);
$newdate=($y+1900). $m . $d;
 
This is a piece of cake using the POSIX module (included in the Perl distribution).

use POSIX;
$newdate = POSIX::strftime(&quot;%Y-%m-%d&quot;,localtime());

will do what you want.

Hope this helps,

brendanc@icehouse.net
 
Why are you using the same variable name ($d) four times in this line:
($d,$d,$d,$d,$m,$y)=localtime();

Check out Thread219-207320 for a bunch of ways to format the date.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top