1

Is there a way to automatically output the date based on the week number?

More precisely: I enter the week number for a specific year and as an output I get the date from Monday to Sunday?

There are several packages for creating calendars, none of them seem to allow this. Does anyone have a good tip?

(Taco Hoekwater did something near to my question, but the other way around: Macro for week number)

1
  • Have a look at Nicola Talbot'S package datetime2, which should provide almost everything: ctan.org/pkg/datetime2
    – MS-SPO
    Commented Apr 17 at 9:03

1 Answer 1

2

I've also added the reverse to PGFCalendar but we can use it and a bit of math to get the correct start and end day of the required week.

Both PGFCalendar and my extension TikZ-ext don't provide a high-level interface for any of this so we have to juggle some counts, values and conversions.

This will not check whether a given week is actually in the year. You could use \weekinyear{174}{2021} to get the same result as the one in the code below.

Code

\documentclass{article}
\usepackage{pgfcalendar-ext}
\makeatletter
\newcommand*\weekinyear[2]{% #1 = week, #2 = year
  \pgfcalendardatetojulian{#2-01-01}{\count@}%
  \edef\wiy@first{\the\count@}%
  \pgfcalendarjuliantoweekday{\count@}{\count@}%
  \edef\wiy@day{\the\count@}%
  \pgfcalendarjulianyeartoweek{\wiy@first}{#2}{\count@}%
  \ifnum\count@>1 \count@=0 \fi% first day might be in last week of prev year
  \pgfintset\count@{\wiy@first+(#1-\count@)*7-\wiy@day}%
  \pgfcalendarjuliantodate{\count@}{\wiyStartY}{\wiyStartM}{\wiyStartD}%
  \advance\count@ by 6
  \pgfcalendarjuliantodate{\count@}{\wiyEndY}{\wiyEndM}{\wiyEndD}%
}
\makeatother
\begin{document}
\weekinyear{18}{2024}
Week 18 in year 2024 starts on
Monday \wiyStartY-\wiyStartM-\wiyStartD\ and
ends on Sunday \wiyEndY-\wiyEndM-\wiyEndD.
\end{document}

Output

Week 18 in year 2024 starts on Monday 2024-04-29 and ends on Sunday 2024-05-05.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .