Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl Date formatting

         

adni18

1:37 am on Oct 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a script.

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
print "$mon/$mday/$year";

but that code always gives me 9/8/104. How can I format it so it is MM/DD/YY? I really need an answer fast. Thanks in advance

Brett_Tabke

1:52 am on Oct 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



year is an offset from 1900.

add:

$year=$year+1900;

adni18

2:05 am on Oct 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what about the 9? shouldn't it be 10 because of october?

Brett_Tabke

2:22 am on Oct 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



remember - programmers start from 0.

cgikira

4:25 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



I'd really suggest installing Date::Format (from CPAN) if you don't already have it. It really saves a lot of time when formatting dates:

#!/usr/bin/perl
use Date::Format;

print time2str("%D", time), "\n";
# (prints 10/09/04)

print time2str("%m/%d/%Y", time), "\n";
# (prints 10/09/2004)

g1smd

6:15 pm on Oct 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmmm, a date like 9/8/04 will be read as 9th Aug in some places and as Sept 8th in others.

Look up ISO 8601 and RFC 3339 for the International Format, which is always YYYY-MM-DD.