[Tex/LaTex] Add (n) working days to a specific date

calculationsdatetime

My g-brief will contain something like "I have noted a deadline for your answer of ten working days until…".

Is there a package or way to add n working-days (ie. mon-fri, let's not over complicate things with holidays etc 😉 to todays date programmatically?

I have found an approximate solution in the datenum and advdate packages:

\setdatetoday
\addtocounter{datenumber}{10}
\setdatebynumber{\thedatenumber}

But this "only"* adds the days, not taking into account if a day is a working day or a free day (ie. weekend).

*In the humble "I couldn't have done anything remotely like this myself (so I'm thankful for it), but it doesn't quite fit my need and possibly could be tweaked"-way.

Best Answer

Here's a solution using the pgfcalendar package (part of the pgf bundle.)

\documentclass{article}

\usepackage{pgfkeys,pgfcalendar}

\newcount\julianday
\newcount\daycount
\newcount\weekday

\newcommand*{\adddays}[2]{%
  \pgfcalendardatetojulian{#1}{\julianday}%
  \daycount=#2\relax
  \loop
    \advance\julianday by 1\relax
    \pgfcalendarjuliantoweekday{\julianday}{\weekday}%
    \ifnum\weekday<5\relax
      % It's a weekday (Mon-Fri)
      \advance\daycount by -1\relax
    \fi
  \ifnum\daycount > 0
  \repeat
  \pgfcalendarjuliantodate{\julianday}{\thisyear}{\thismonth}{\thisday}%
  \thisyear-\thismonth-\thisday
}

\begin{document}

10 working days from
\the\year-\the\month-\the\day\ (today):
\adddays{\year-\month-\day}{10}

\end{document}

This produces:

10 working days from 2014-4-16 (today): 2014-04-30

You can adapt this to take holidays into account:

\documentclass{article}

\usepackage{pgfkeys,pgfcalendar}
\usepackage{etoolbox}

\newcount\julianday
\newcount\daycount
\newcount\weekday

\newcommand*{\holiday}[2]{%
  \pgfcalendardatetojulian{#1}{\julianday}%
  \csdef{holiday-\number\julianday}{#2}%
}

\newcommand*{\adddays}[2]{%
  \pgfcalendardatetojulian{#1}{\julianday}%
  \daycount=#2\relax
  \loop
    \advance\julianday by 1\relax
    \pgfcalendarjuliantoweekday{\julianday}{\weekday}%
    \ifnum\weekday<5\relax
      % It's a weekday (Mon-Fri)
      \ifcsdef{holiday-\number\julianday}%
      {% It's a holiday
      }%
      {% Not a holiday
        \advance\daycount by -1\relax
      }%
    \fi
  \ifnum\daycount > 0
  \repeat
  \pgfcalendarjuliantodate{\julianday}{\thisyear}{\thismonth}{\thisday}%
  \thisyear-\thismonth-\thisday
}

\holiday{2014-04-21}{Easter Monday}
\holiday{2014-05-05}{Early May Bank Holiday}

\begin{document}

10 working days from
\the\year-\the\month-\the\day\ (today):
\adddays{\year-\month-\day}{10}

\end{document}

This produces:

10 working days from 2014-4-16 (today): 2014-05-01