# Add (n) working days to a specific date

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.

• see the following question: tex.stackexchange.com/questions/4873/date-calculations – Uwe Ziegenhagen Apr 16 '14 at 13:10
• If you are only worried about 10 days, isn't this always 2 weeks, and you can just add 14 normal days? – cslstr Apr 16 '14 at 13:11
• @cslstr Not if you start on a weekend. – Steven B. Segletes Apr 16 '14 at 13:20
• @StevenB.Segletes Exactly - I don't know when the letter will be written. – Christian Apr 16 '14 at 18:01
• Oh I see. I didn't see the "workday" requirement. The scrdate package can tell you the weekday for a specific date. I guess that and advdate could bring a suitable way (it's beyond my knowledge) – Uwe Ziegenhagen Apr 16 '14 at 18:46

## 1 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:

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:

• 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 9:40
• That goddess-comment might be read wrongly. You know what I mean. – Christian Apr 17 '14 at 9:41
• @Christian LOL :-) – Nicola Talbot Apr 17 '14 at 10:01
• @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 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