Google Groups Home
Help | Sign in
Message from discussion FAQ 4.12 How do I find the day or week of the year?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
PerlFAQ Server  
View profile
 More options May 10, 3:03 pm
Newsgroups: comp.lang.perl.misc
From: PerlFAQ Server <br...@stonehenge.com>
Date: Sat, 10 May 2008 12:03:02 -0700
Local: Sat, May 10 2008 3:03 pm
Subject: 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 "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 ) );

    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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google