Forum Moderators: coopster & phranque

Message Too Old, No Replies

parsing through dates

         

sumeet ruiwale

5:43 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



i need some help. i want a function which will parse through number of dates and pick the latest on and store it back ona excel sheet.
the dates r in a mm/dd/yyyy format. i want the function to pcik up the latest date from the dates on the webpage.

can anyone help me with that.

lexipixel

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

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


The easiest way to sort dates is to convert them to YYYYMMDD format. Be sure to "zero left pad" the MM and DD portions of the date.

Read in the dates and store into an array.

You can then use "sort" and end up with a list of
dates that look like:

@dates = sort @dates;

20020101
20021225
20040615
20050801
20050805
20050811

in this case the last date is the newest, but you could just as easily use "reverse" on the sort.

moltar

10:52 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What lexipixel said.

You can parse and turn the date into the YYYYMMDD format with the following code:


my $date = '11/4/2005';
print sprintf("%4d%02d%02d", ((split('/', $date))[2,0,1]));

s1developer

12:33 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



You could also use the (standard) Date::Parse module for this. It will handle other date formats too so it might be worth considering:

#-----------
use Date::Parse 'str2time';
my $time = str2time('8/12/2005');
#-----------

That will give you a 'time' value, i.e. a big number representing the number of seconds since 1 Jan 1970. So you could get the most recent date in a one-r like this:

#-----------

use Date::Parse 'str2time';

my $most_recent = (reverse sort {str2time($a) <=> str2time($b)} @dates)[0];
#-----------

which is arguably a bit cleaner than using two sprintf commands, at the (small) expense of loading the Date::Parse module.

Anyway just another option.

Ref [search.cpan.org...]

FiRe

2:21 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



A very good module is Date::Manip take a look at it...