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.

my $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 "POSIX"'s "mktime" to get a
time in epoch seconds for the argument to "localtime".

use POSIX qw/mktime strftime/;
my $week_of_year = strftime "%W",
localtime( mktime( 0, 0, 0, 18, 11, 87 ) );

You can also use "Time::Piece", which comes with Perl and provides a
"localtime" that returns an object:

use Time::Piece;
my $day_of_year = localtime->yday;
my $week_of_year = localtime->week;

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

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 4.12 How do I find the day or week of the year?
    ... comes with the standard Perl distribution. ... The localtime function returns the day of the year. ... The "POSIX" module can also format a date as the day of the year or week ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)
  • FAQ 4.12 How do I find the day or week of the year?
    ... comes with the standard Perl distribution. ... The localtime function returns the day of the year. ... The "POSIX" module can also format a date as the day of the year or week ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)
  • FAQ 4.12 How do I find the day or week of the year?
    ... comes with the standard Perl distribution. ... The localtime function returns the day of the year. ... The "POSIX" module can also format a date as the day of the year or week ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)
  • FAQ 4.12 How do I find the day or week of the year?
    ... comes with the standard Perl distribution. ... The localtime function returns the day of the year. ... The "POSIX" module can also format a date as the day of the year or week ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)
  • FAQ 4.12 How do I find the day or week of the year?
    ... comes with the standard Perl distribution. ... The localtime function returns the day of the year. ... The "POSIX" module can also format a date as the day of the year or week ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)