FAQ 4.12 How do I find the day or week of the year?



This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.12: How do I find the day or week of the year?

The localtime function returns the day of the year. Without an argument
localtime uses the current time.

$day_of_year = (localtime)[7];

The POSIX module can also format a date as the day of the year or week
of the year.

use POSIX qw/strftime/;
my $day_of_year = strftime "%j", localtime;
my $week_of_year = strftime "%W", localtime;

To get the day of year for any date, use the Time::Local module to get a
time in epoch seconds for the argument to localtime.

use POSIX qw/strftime/;
use Time::Local;
my $week_of_year = strftime "%W",
localtime( timelocal( 0, 0, 0, 18, 11, 1987 ) );

The Date::Calc module provides two functions to calculate these.

use Date::Calc;
my $day_of_year = Day_of_Year( 1987, 12, 18 );
my $week_of_year = Week_of_Year( 1987, 12, 18 );



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every operating
system or platform, so please include relevant details for corrections
to examples that do not work on particular platforms. Working code is
greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
.



Relevant Pages

  • FAQ 8.5 How do I read just one key without waiting for a return key?
    ... comes with the standard Perl distribution. ... your system supports POSIX). ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ... so please include relevant details for corrections ...
    (comp.lang.perl.misc)
  • FAQ 4.53 How do I manipulate arrays of bits?
    ... comes with the standard Perl distribution. ... or else vec() and the bitwise operations. ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ... so please include relevant details for corrections ...
    (comp.lang.perl.misc)
  • FAQ 5.7 How can I manipulate fixed-record-length files?
    ... comes with the standard Perl distribution. ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ... system or platform, so please include relevant details for corrections ...
    (comp.lang.perl.misc)
  • FAQ 8.9 How do I ask the user for a password?
    ... comes with the standard Perl distribution. ... reduce the number of repeated questions as well as allow the community ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ... so please include relevant details for corrections ...
    (comp.lang.perl.misc)
  • FAQ 8.15 How do I set the time and date?
    ... comes with the standard Perl distribution. ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ... system or platform, so please include relevant details for corrections ...
    (comp.lang.perl.misc)