Forum Moderators: coopster & phranque

Message Too Old, No Replies

how to convert yyyymmdd to yyyy,mm,dd

         

sumeet ruiwale

6:05 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



can anyone tell me how would i convert yyyymmdd to yyyy,mm,dd. Like i am getting a date as 20051004 which i want like 2005,10,04. Can anyone help me with that.

ChadSEO

7:05 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



sumeet_ruiwale,

The following should do the trick, assuming it is a valid date:

$date =~ s/([0-9]{4})([01][0-9])([0-3][0-9])/\1,\2,\3/;

Chad

sumeet ruiwale

8:11 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



can u plz explain in detail?
i have my $latest_date = shift@dates, were its grabing the latest date. this is in yyyymmdd format. i want to change it to yyyy,mm,dd format.

ChadSEO

8:28 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



The code that you have now pulls a date from an array:

$latest_date = shift @dates;

After that, you can use the code I posted to transform this from yyyymmdd to yyyy,mm,dd, like so:

$latest_date =~ s/([0-9]{4})([01][0-9])([0-3][0-9])/\1,\2,\3/;

Essentially what this does is groups together 4 numbers, 2 numbers, then 2 numbers, and inserts a comma between each group. Hope that clears things up a bit; if not, let me know :)

Chad

lexipixel

5:07 pm on Aug 18, 2005 (gmt 0)

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



Here's a substring method --- this may be easier to understand (it stores 'yyyy', 'mm' and 'dd' into three seperate variables).

#
$yyyy = substr($yyyymmdd,0,4);
$mm = substr($yyyymmdd,4,2);
$dd = substr($yyyymmdd,6,2);
#

KevinADC

6:13 pm on Aug 18, 2005 (gmt 0)

10+ Year Member



if you know your date strings are in the correct format before you insert the commas, you can use a simplified form of the regexp posted earlier:


[3]
my $date = 20051004;
$date =~ s/(.{4})(.{2})(.{2})/\1,\2,\3/;
print $date;[/3]

although I think it is a good idea to check the format first. I would only do this if I knew for sure the dates were yyyymmdd formatted.

ChadSEO

6:42 pm on Aug 18, 2005 (gmt 0)

10+ Year Member



If you know they're the right format, an even simpler one would be:

my $date = 20051004;
$date =~ s/(....)(..)(..)/\1,\2,\3/;
print $date;

Saves a whole 4 bytes, and we all know how precious disk space is these days :)

Chad

KevinADC

8:09 pm on Aug 18, 2005 (gmt 0)

10+ Year Member




Saves a whole 4 bytes, and we all know how precious disk space is these days :)

hehehe.... my 80 GB HDD is full of nooks and crannies.