# How to calculate future dates based on custom date (e.g. 2018-05-05 + 14 days)?

I want to create invoices for customers in scrlttr2. Hereby I set the document date via \setkomavar{date}{\today}.

I reuse this date with advdate to automatically add a payment deadline into the letter foot (deadline = todays date + 14 days in future).

This is working completely fine if only using \today as the documents date (see MWE).

Minimum Working Example (MWE):

\documentclass{scrlttr2}

\setkomavar{date}{\today}

\begin{document}
\end{document}


However, I also want to use custom dates, e.g. 2018-05-05 as the document date. Do you have any idea on how to generate the mentioned payment deadline based on arbitrary dates instead of \today only?

In best case I could insert \today as well as 2018-05-05 into the \setkomavar{date}-command, so the following example should add 14 days to my custom date (when using custom dates) but to \today's date as well (in case of using \today as the document date):

\documentclass{scrlttr2}

\setkomavar{date}{2018-05-05}

\begin{document}
\end{document}


• advdate (it seems like this only works for \today)
• datenumber (difficult to use without \today)
• datetime or datetime2 (also easiest to use with \today)

First of all there is a difference between \today and the content of the variable date. Setting the variable date does not change \today. See the following example:

\documentclass{scrlttr2}
\begin{document}
% default value of date variable is \today
Today is \today{} and the variable date stores \usekomavar{date}.

% changing value for variable date does not change \today!
\setkomavar{date}{2018-05-05}
Today is \today{} and the date variable stores \usekomavar{date}.
\end{document}


Result

Package advdate provides the additional commands \SaveDate, \SetDate and \DayAfter (see the package documentation), which can be used to get the desired result.

First of all we save todays date as dd/mm/yyyy (this syntax is used by all macros of advdate) in the macro \invoicedate.

% Save dd/mm/yyyy for today
\newcommand*\invoicedate{}
\SaveDate[\invoicedate]


\invoicedate then will be used to set the set the KOMA-Script variable date and a new variable paymentdate automatically. You do not have to set or change these variables manually!

\setkomavar{date}{{\SetDate[\invoicedate]\today}}
\newkomavar{paymentdate}
\setkomavar{paymentdate}{{\SetDate[\invoicedate]\DayAfter[14]}}


Only if you want to set a fixed value for variable date (and the depending variable paymentdate) you have to use the following macro\setinvoicedate. Note that this macro changes \invoicedate and sets the KOMA-Scripts variables date and paymentdate automatically. So you only have to use this macro for a diffent invoice/letter date.

\newcommand*\setinvoicedate[1]{%
\ifstr{#1}{today}
{\SaveDate[\invoicedate]}%
{\renewcommand*\invoicedate{#1}}%
}


The syntax the is \setinvoicedate{dd/mm/yyyy}. Example:

\setinvoicedate{24/12/2018}


You can use \setinvoicedate several times in a document, ie if you have more than one invoice in your document. If you want to reset the invoice/letter date to todays date, you can use

\setinvoicedate{today}


Complete example:

\documentclass{scrlttr2}

% Save dd/mm/yyyy for today
\newcommand*\invoicedate{}
\SaveDate[\invoicedate]

\setkomavar{date}{{\SetDate[\invoicedate]\today}}
\newkomavar{paymentdate}
\setkomavar{paymentdate}{{\SetDate[\invoicedate]\DayAfter[14]}}

% should be possible to use a different invoicedate
\newcommand*\setinvoicedate[1]{%
\ifstr{#1}{today}
{\SaveDate[\invoicedate]}%
{\renewcommand*\invoicedate{#1}}%
}
% set a different invoice date
% syntax: \setinvoicedate{dd/mm/yyyy}
% if it should be reset to \today use \setinvoicedate{today}
\setinvoicedate{24/12/2018}

\newcommand\demo{% only for this example
\begin{itemize}
\item The invoice date is \usekomavar{date}.
\item But today is the \today.
\end{itemize}%
\bigskip
}

\begin{document}
\demo

\setinvoicedate{11/03/2019}%
Change the invoice date  to \usekomavar{date}: \demo

\setinvoicedate{today}%
Reset the invoice date to today: \demo
\end{document}


Or with class option numericaldate:

Or with package isodate to format the date output:

Code:

\documentclass[english]{scrlttr2}
\usepackage{isodate}
\isodate

% Save dd/mm/yyyy for today
\newcommand*\invoicedate{}
\SaveDate[\invoicedate]

% should be possible to use a different invoicedate
\newcommand*\setinvoicedate[1]{%
\ifstr{#1}{today}
{\SaveDate[\invoicedate]}%
{\renewcommand*\invoicedate{#1}}%
}

\setkomavar{date}{{\SetDate[\invoicedate]\today}}
\newkomavar{paymentdate}
\setkomavar{paymentdate}{{\SetDate[\invoicedate]\DayAfter[14]}}

% set a different invoice date
% syntax: \setinvoicedate{dd/mm/yyyy}
% if it should be reset to \today use \setinvoicedate{today}
\setinvoicedate{24/12/2018}

\newcommand\demo{% only for this example
\begin{itemize}
\item The invoice date is \usekomavar{date}.
\item But today is the \today.
\end{itemize}%
\bigskip
}

\begin{document}
\demo

\setinvoicedate{11/03/2019}%
Change the invoice date  to \usekomavar{date}: \demo

\setinvoicedate{today}%
Reset the invoice date to today: \demo
\end{document}


Update regarding a comment below:

To avoid payment dates on saturdays or sundays you can load package scrdate and use

\setkomavar{paymentdate}{{%
\SetDate[\invoicedate]%
\ifnum\todaysnumber=6\DayAfter[16]% Saturday -> Monday
\else\ifnum\todaysnumber=0\DayAfter[15]% Sunday -> Monday
\else\DayAfter[14]
\fi\fi
}}


Example:

\documentclass{scrlttr2}

% Save dd/mm/yyyy for today
\newcommand*\invoicedate{}
\SaveDate[\invoicedate]

\setkomavar{date}{{\SetDate[\invoicedate]\today}}
\newkomavar{paymentdate}
\setkomavar{paymentdate}{{% <- changed
\SetDate[\invoicedate]%
\ifnum\todaysnumber=6\DayAfter[16]% Saturday -> Monday
\else\ifnum\todaysnumber=0\DayAfter[15]% Sunday -> Monday
\else\DayAfter[14]
\fi\fi
}}

% should be possible to use a different invoicedate
\newcommand*\setinvoicedate[1]{%
\ifstr{#1}{today}
{\SaveDate[\invoicedate]}%
{\renewcommand*\invoicedate{#1}}%
}
% set a different invoice date
% syntax: \setinvoicedate{dd/mm/yyyy}
% if it should be reset to \today use \setinvoicedate{today}
\setinvoicedate{11/08/2018}

\newcommand\demo{% only for this example
\begin{itemize}
\item The invoice date is \usekomavar{date}.
\item But today is the \today.
\end{itemize}%
\bigskip
}

\begin{document}
\demo

\setinvoicedate{12/08/2019}%
Change the invoice date  to \usekomavar{date}: \demo

\setinvoicedate{13/08/2018}%
Change the invoice date  to \usekomavar{date}: \demo

\setinvoicedate{today}%
Reset the invoice date to today: \demo
\end{document}


• Thanks a lot for your awesome work! Would there be an option to copy the value for \setinvoicedate from scrlttr2's date-option? In this case I would have to insert the desired date only one time...
– Dave
Aug 3 '18 at 20:59
• No, you can not use KOMA-Script variable date as argument of \setinvoicedate. But \setinvoicedate sets the value of date and the depending variable paymentdate automatically. So you do not have to insert the date more than once! See my updated answer, I have added some additional explanations.
– esdd
Aug 4 '18 at 10:50
• Excuse me @esdd, just one more (small) thing: How can I adjust your date command so that paymentdate's are always on working days? For example, if the payment date would be calculated on a saturday or sunday, it should be rounded up to monday.
– Dave
Aug 14 '18 at 8:18
– esdd
Aug 14 '18 at 23:50

Here's a simple version with datetime2 which is based on this answer. Please note that using the KOMA variable date is not as easy as it seemed and I introduced a separate command for that (which is \edefed to allow \today as value). This will be used for setting the KOMA variable and doing calculations.

\documentclass{scrlttr2}
% from https://tex.stackexchange.com/a/237129
\usepackage[calc,useregional]{datetime2}
\newcount\myct
\newcount\datecount
\newcommand{\myday}[2][0]{%
\DTMsavedate{mydate}{#2}%
\DTMsaveddateoffsettojulianday{mydate}{#1}{\myct}%
\DTMsavejulianday{mydate}{\number\myct}%
\DTMusedate{mydate}%
}

\edef\tmpdate{2018-05-05}
\setkomavar{date}{\tmpdate}

\begin{document}
Today is \today. Please pay until \myday[14]{2018-05-05}. This is still \today. \myday[14]{\tmpdate}
\end{document}

• @Dave No problem, as I mentioned in the question's comments this is only a quick and dirty adaption of another answer, not a thoroughly designed approach to address specifically this issue. Aug 4 '18 at 6:46