How to calculate future dates based on custom date (e.g. 2018-05-05 + 14 days)?
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}
\usepackage{advdate}
% 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 Please pay until \usekomavar{paymentdate}.
\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
\usepackage{advdate}
% 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 Please pay until \usekomavar{paymentdate}.
\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}
\usepackage{scrdate}% <- added
\usepackage{advdate}
% 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 Please pay until \usekomavar{paymentdate}.
\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}
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 \edef
ed 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}