This is a small Perl script to calculate the number of the week from any given date, (given in the ISO 8601 format), or uses today's date if no date is specified. It was written just to scratch an itch I had in the past, when I frequently needed to know the number of the week for planning purposes.

See the POD for more information about how it works. This is the first time I share my code here on PerlMonks, and any comments, suggestions or improvements are greatly appreciated.

#!/usr/bin/perl -w # # calcweek.pl - Calculate the week number from any given date, # or the default, if no date is given. # # Sander Venema <sander.venema@gmail.com> version 20080819 use strict; use Getopt::Long; use Pod::Usage; use Data::Dumper; use Date::ISO; use POSIX qw(strftime); # set some defaults my $verbose = 0; my $date = strftime("%Y%m%d", localtime); # fetch options Getopt::Long::Configure(qw(no_ignore_case bundling)); GetOptions( 'h|help' => sub { pod2usage({-verbose => 1, -input => \*DAT +A}); exit; }, 'H|man' => sub { pod2usage({-verbose => 2, -input => \* +DATA}); exit; }, 'v|verbose' => \$verbose, 'd|date=i' => \$date, ) or pod2usage(2); # convert date string to Date::ISO object and calculate the # year, week and number of the day (starting with Monday) my $iso = Date::ISO->new(iso => $date); my ($year, $week, $week_day) = ($iso->year, $iso->iso_week, $iso->iso_ +week_day); # print everything, depending on level of $verbose print "$year-W$week-$week_day\n" if $verbose; print "$week\n" if !$verbose; __DATA__ =head1 NAME calcweek.pl - Calculate the week number from any given date =head1 SYNOPSIS calcweek.pl [-h] [-H] [-v] [-d yyyymmdd] =head1 DESCRIPTION This is a small Perl script to calculate the number of the week from a +ny given date, (given in the ISO 8601 format), or uses today's date if no date is spe +cified. It was written just to scratch an itch I had in the past, when I frequently n +eeded to know the number of the week for planning purposes. The program returns the number of the week it is, and if verbose is tu +rned on, it will print the year, weeknumber, and day of the week in ISO 8601 forma +t (YYYY-Www-D). When verbose is turned off, it will only output the number of the week +. The following options are available: -h --help Display this help -H --man Display detailed help (manpage) -v --verbose Turn more verbose messages on -d --date <yyyymmdd> Set the date to calculate (default: tod +ay) =head1 DEPENDENCIES Date::ISO, POSIX, Getopt::Long, Pod::Usage, Data::Dumper =head1 SEE ALSO For more information about ISO 8601: http://www.iso.org/iso/date_and_time_format =head1 BUGS Lots. Probably. Any help on how to improve it is appreciated. :-) =head1 AUTHOR Sander Venema

In reply to calcweek by sv87

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.