from day of year to day, month, year 
Author Message
 from day of year to day, month, year

Hi,
        If I know day of year and year, is there a function in Perl that allows
me to get day, month and year?
        Thanks
wl


Sat, 09 Feb 2002 03:00:00 GMT  
 from day of year to day, month, year

Quote:

> Hi,
> If I know day of year and year, is there a function in Perl that allows
> me to get day, month and year?
> Thanks
> wl

You can use the Date::Manip module (available from CPAN) or my function
below:
#!/usr/bin/perl -w
use strict;

sub ydoy2ymd { # Converts (year, day_of_year) to (year, month, day_of_month)

  my $month;

  $daysinmo[1]++ if !($year % 4) && ($year % 100 || !($year%400));

  for ($month=0; $month<11 && $dayofyear > $daysinmo[$month]; $month++) {
    $dayofyear -= $daysinmo[$month];
  }
  return ($year, $month+1, $dayofyear);

Quote:
}

# Below is for testing and comparison; you only need the function above

use Date::Manip;

for (my $j=0; $j <= 100; $j++) {
  my $y = 1900 + int(rand(102));
  my $d = 1+int(rand(365));
  my $date1 = UnixDate(sprintf("$y-%03d", $d), "%Y-%m-%d");
  my $date2 = sprintf("%d-%02d-%02d", ydoy2ymd($y, $d));
  if ($date1 ne $date2) {
    printf("%d-%03d -> $date1 or $date2 ???\n", $y, $d);
  } else {
    printf("%d-%03d -> $date1\n", $y, $d);
  }

Quote:
}



Sun, 10 Feb 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Convert Day of Year to Month and Day?

2. Converting yy.ddd to month-day-year

3. Converting Month Day Year to time_t.

4. Calculating weekday given year, month and day

5. Extract the day, month, year of a string

6. Q: re: POSIX::mktime and day-of-year

7. counting through days of the year

8. Stupid FAQ question of the (day? month? year?)

9. Date/Time modules - any without default months/days?

10. Finding first day of a month

11. Calandaring in perl - Day of month from date?

12. Day of the month redirection - revised

 

 
Powered by phpBB® Forum Software