View Full Version : getting date (01-31) using year,month,doy

11-18-2009, 08:25 PM
I am on the last date manipulation but I am stuck.

I have year, month and day_of_year and I need to compile an iso format date eg 2009-11-17 currently I have got it to 2009-11-289 :eek: ( It has been a long month but that is kickin' the **** out of it :) )

So can any of you please point me to where there is a conversion in one of the date modules. I have loked in Date:Parse, Date:Calc and as a panic choice; Date:Format. I can't see it or several which in combination might do it.:confused:

here is the code so you can see what I have been up to

my ($first_year, $first_month, $first_day) = split /-/ , $first_date, 3;
my ($last_year, $last_month, $last_day) = split /-/ , $last_date, 3;
my $Dd = '30'; #number_of_days_to_display

my $start_doy = Day_of_Year( $first_year, $first_month, $first_day );

my $last_doy = Day_of_Year( $last_year, $last_month, $last_day );

#calculate last doy from start date for $Dd days
my $number_of_days_to_show = ( $start_doy + $Dd ); # number_of_days_to_show

#Days In first Year
my $days_in_first_year = Days_in_Year( $first_year, '12' );

my $month;
my $year;
my $count=0;

foreach my $day_number ($start_doy .. $last_doy)
print qq( doy = $day_number :: count = $count <br /> );

# work out the year and month here.
#if day_number exceeds days_in_year reset to zero
if ($day_number == $days_in_first_year){

#if day_number exceeds days_in_year use $last_year and $last_month
if ( $day_number > $days_in_first_year ) {
$year = $last_year;
$month = $last_month;
} else {
$month = $first_month;

#set the date (01 - 31) from day_of_year

print qq( $count=$count : day_number = $day_number <br /> );


11-18-2009, 10:36 PM
From Date::Calc

$doy = Day_of_Year($year,$month,$day);

This function returns the (relative) number of the day of the given date in the given year.

E.g., "Day_of_Year($year,1,1)" returns "1", "Day_of_Year($year,2,1)" returns "32", and "Day_of_Year($year,12,31)" returns either "365" or "366".

The day of year is sometimes also referred to as the Julian day (or date), although it has nothing to do with the Julian calendar, the calendar which was used before the Gregorian calendar.

In order to convert the number returned by this function back into a date, use the function "Add_Delta_Days()" (described further below), as follows:

$doy = Day_of_Year($year,$month,$day);
($year,$month,$day) = Add_Delta_Days($year,1,1, $doy - 1);

12-07-2009, 10:52 PM
Thanks FishMonger - belatedly. Got carried away with the answer and forgot to come back until now.


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum