I am working on a weekly teaching plan. I need the start date and end date of each week. For example, week 30 I want 7/25 - 7/29, week 31 I want 1/8 - 5/8. I don't want to do this manually, because it takes a long time. Is there a convenient way to get the first and last day of a week? Thank you.
1 Answer
Specifying the first day of the first week manually
You can use pgfcalendar
to store the first day of your first week in a LaTeX counter and use that later as the base for an offset.
With \setStartDate
you set the first day of your first week and the \getDateOfWeek{<week>}{<offset>}
will define \Year
, \Month
, \Day
with the date that is in the <week>
th week with an additional <offset>
.
For example:
\setStartDate{2022-10-10}
sets the first week to start on October 10th, 2022.
With
\getDateOfWeek{1}{4}
you'll get October 14th and with
\getDateOfWeek{2}{0}
you'll get October 17th in \Month
and \Day
.
Code
\documentclass{article}
\usepackage{pgfcalendar}
\newcounter{myStartDate}
\makeatletter
\newcommand*{\setStartDate}[1]{% for week 1
\begingroup
\pgfcalendardatetojulian{#1}{\@tempcnta}%
\setcounter{myStartDate}{\@tempcnta}%
\endgroup
}
\newcommand*{\getDateOfWeek}[2]{%
\begingroup
\@tempcnta=\inteval{(#1-1)*7+#2}\relax
\advance\@tempcnta by \c@myStartDate
\edef\@temp{\endgroup\noexpand\pgfcalendarjuliantodate{\the\@tempcnta}}%
\@temp{\Year}{\Month}{\Day}%
}
\makeatother
\usepackage{pgffor}
\begin{document}
\setStartDate{2022-01-03}
\foreach \week in {1,...,52}{
Week \week\ goes from \getDateOfWeek{\week}{0}\Month/\Day\ to
\getDateOfWeek{\week}{4}\Month/\Day.\par}
\end{document}
Output
Using the actual week numbers of the years.
With the pgfcalendar-ext
package of my tikz-ext
package which incorporates another answer of mine and uses the week numbering according to ISO 8601 which mainly means that a new week starts on Monday.
Here's a solution with
\getTwoDaysOfWeek[<year>]{<week>}{<offset>}
{<year>}{<month>}{<day>}{<offset year>}{<offset month>}{<offset day>}
which defines six macros (the argments on the second line) for you which contain
- the first day of
<week>
of<year>
and - the day that's
<offset>
after the first day of<week>
of<year>
.
The <year>
argument is optionally, the default is the current year.
Code
\documentclass{article}
\usepackage{pgfcalendar-ext}
\newcommand*{\stripZero}[1]{\if0#1\else#1\fi}
\makeatletter
\newcommand*{\getTwoDaysOfWeek}[9][\the\year]{%
% #1 = year (defaults to current year)
% #2 = week number, #3 = offset
% #4/#5/#6 = start year/month/day
% #7/#8/#9 = end year/month/day
\begingroup
\pgfcalendardatetojulian{#1-01-01}{\@tempcnta}%
\pgfcalendarjuliantoweekday{\@tempcnta}{\@tempcntb}%
\edef\@dateStartWeek{\the\numexpr\@tempcnta-\@tempcntb+#2*7-7\relax}%
\pgfcalendarjulianyeartoweek{\@tempcnta}{#1}{\@tempcntb}%
\ifnum\@tempcntb>1
% This is the last week of the previous year
\edef\@dateStartWeek{\the\numexpr\@dateStartWeek+7\relax}%
\fi
\edef\@temp{\endgroup%
\noexpand\pgfcalendarjuliantodate
{\@dateStartWeek}{\noexpand#4}{\noexpand#5}{\noexpand#6}%
\noexpand\pgfcalendarjuliantodate
{\the\numexpr\@dateStartWeek+#3\relax}{\noexpand#7}{\noexpand#8}{\noexpand#9}}%
\@temp
}
\makeatother
\newcommand*{\weekCell}[2][\the\year]{%
\begin{tabular}[t]{@{}l@{}}Week #2:\\
\getTwoDaysOfWeek[#1]{#2}{4}{\y}{\m}{\d}{\Y}{\M}{\D}%
\stripZero\m/\stripZero\d--\stripZero\M/\stripZero\D
\end{tabular}}
\usepackage{pgffor}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{lll}
\toprule
Time & Content & Note \\\midrule
\weekCell{1} & Content 1 & Note 1 \\
\weekCell{2} & Content 2 & Note 2 \\
\dots & \dots & \dots \\\bottomrule
\end{tabular}
\end{document}
pythontex
and its datetime modulecalendar
library of TikZ, and Section 89 Date and Calendar Utility Macros for the packagepgfcalendar
. This package can be used independently of pgf.