7

How can I specify a relative date and let LaTeX insert the resulting absolute one?

For example like this:

\somedatecommand{next thursday}

Which should result in:

Thursday, 20.10.2011

Or:

20.10.2011

Great would be also babel support (e.g. for non-english documents).

maxschlepzig
  • 12,082

1 Answers1

7

You could use the datetime package to format the dates and quoting form the package documentation:

As from version 2.42, the datetime package is now compatible with babel, however you must load the datetime package after the babel package.

The harder part is determining the date to print. This can be achieved using \AdvanceDate[n] macro from the advdate package to advance the date by a specified number of days. Then it is just a matter of determining how many days we need to advance to get to "next day-of-week".

To start from a day other than the current day one can use \SetDate[dd/mm/yyyy].

enter image description here

\documentclass{article}
\usepackage{xstring}% Needed for \IfStrEqCase
\usepackage{datenumber}% Date formatting
\usepackage{advdate}% Needed for \AdvanceDate
\usepackage{pgf}% For math
\usepackage[ddmmyyyy]{datetime}
\newdateformat{mydate}{\twodigit{\THEDAY}.\twodigit{\THEMONTH}.\THEYEAR}

\newcounter{dateOffset}%
\newcommand*{\SetDateOffsetForNext}[1]{%
\pgfmathsetcounter{dateOffset}{7-int(\the\value{datedayname})}% Initialize
\IfStrEqCase{#1}{%
    {mon}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+1,7))}}%
    {tue}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+2,7))}}%
    {wed}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+3,7))}}%
    {thu}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+4,7))}}%
    {fri}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+5,7))}}%
    {sat}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+6,7))}}%
    {sun}{\pgfmathsetcounter{dateOffset}{int(mod(\the\value{dateOffset}+7,7))}}%
    }[\PackageError{\SetDateOffsetForNext}{Do not know "#1" as day of week}{}]%
    \IfEq{\the\value{dateOffset}}{0}{\pgfmathsetcounter{dateOffset}{7}}{}%
}%

\newcommand*{\PrintDateForNext}[1]{%
    \SetDateOffsetForNext{#1}% Determine date offset
    \AdvanceDate[\value{dateOffset}]% Advance to specified day
    \mydate\today% Print specified date
    \AdvanceDate[-\value{dateOffset}]% Restore current day
    is next #1\par% debug output
}

\begin{document}
\noindent
Working from current day:
Today is \mydate\today\par
\PrintDateForNext{mon}\par
\PrintDateForNext{tue}\par
\PrintDateForNext{wed}\par
\PrintDateForNext{thu}\par
\PrintDateForNext{fri}\par
\PrintDateForNext{sat}\par
\PrintDateForNext{sun}\par

\bigskip\noindent
Back to ``May the Fourth Be With You":
\SetDate[04/05/1977]% dd/mm/yyyy
Today is \mydate\today\par
\PrintDateForNext{mon}\par
\PrintDateForNext{tue}\par
\PrintDateForNext{wed}\par
\PrintDateForNext{thu}\par
\PrintDateForNext{fri}\par
\PrintDateForNext{sat}\par
\PrintDateForNext{sun}\par
\end{document}
Peter Grill
  • 223,288
  • Awesome. I have to remember the \pgfmathsetcounter command. – maxschlepzig Oct 20 '11 at 08:27
  • @Peter Grill, I want to use this code, but I would like to change the date for today, I mean I would like to set '\setdate[02/01/2015]' and after that use '\PrintDateForNext{mon}'. How can I do it? – Gitano Jan 09 '15 at 05:45
  • @Gitano: You can use \SetDate[dd/mm/yyyy]. Have updated the solution to illustrate this. – Peter Grill Jan 09 '15 at 06:19
  • @PeterGrill, thanks, but there is a problem, 08.05.1977 is sunday. Now I understand why I had problems. Do you know why? – Gitano Jan 09 '15 at 06:39
  • @Gitano: Sorry don't know off hand. I'd suggest you post a separate question. Apologies for not testing it thoroughly. – Peter Grill Jan 09 '15 at 07:01