FAQ 4.12 How do I find the day or week of the year?
- From: PerlFAQ Server <comdog@xxxxxxxx>
- Date: Sat, 28 Jan 2006 05:03:01 +0000 (UTC)
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.
.
- Prev by Date: Re: displaying different content types in same page
- Next by Date: Re: string manipulation
- Previous by thread: Re: protect perl script from spammers
- Next by thread: FAQ 4.73 How do I print out or copy a recursive data structure?
- Index(es):
Relevant Pages
|