9

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.

Christian
  • 1,435

1 Answers1

7

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

Nicola Talbot
  • 41,153
  • 1
    You are the goddess of LaTeX. Thank you! I have added a little variable \newcommand{\FristinWerktagen}{10} ("DeadlineInWorkingDays") which I define at the top and in your command and I have turned \thisyear-\thismonth-\thisday into \thisday.\thismonth.\thisyear (my inner nerd prefers the first, but real people need to read this ;) and it's perfect. Again, Thank you! – Christian Apr 17 '14 at 09:40
  • That goddess-comment might be read wrongly. You know what I mean. – Christian Apr 17 '14 at 09:41
  • 1
    @Christian LOL :-) – Nicola Talbot Apr 17 '14 at 10:01
  • 1
    @Christian One day I'll finish my new LaTeX book which has a chapter on using pgfcalendar. At the moment I only have an incomplete draft version available at http://www.dickimaw-books.com/latex/admin/admin-report-draft.pdf – Nicola Talbot Apr 17 '14 at 10:29
  • If that is considered an "incomplete draft" the finished product will be amazing. PS. The @-Thingie doesn't work with your name for me. Time to head to some meta stackexchange site... Happy easter! – Christian Apr 17 '14 at 13:47