# How to specify a date and make calculations with it in datetime2?

I want to specify a date (and give it a name) in a master header TEX-file, then refer to that date in several independent daughter files. As this doesn't even work within a single file, this simplifies to the following MWE:

% Compile with XeLaTeX

\documentclass[a4paper,12pt,]{article}

\usepackage{polyglossia}
\usepackage[british]{datetime2}
\usepackage{datetime2-calc}

\begin{document}

The present date: \DTMdate{2018-01-24}

Two weeks later: \DTMdate{2018-01-24+14}

Beautiful!

\DTMsavedate{RandomDate}{1991-02-03}

% FAULTY LINES JUST TO SHOW WHAT I TRIED THAT DOESN'T WORK
%\DTMsavedate{RandomDate1}{\DTMdisplaydate{2004}{05}{06}{3}}
%\DTMsavedate{RandomDate2}{\DTMdate{2017-08-09}}
% AND OF COURSE IT DOESN'T, BUT AT LEAST I TRIED.
% IT WOULD HAVE BEEN TOO EASY! :D

A random date \DTMusedate{RandomDate}

%Two weeks later: \DTMdate{RandomDate+14} <------------------------------PROBLEM

\end{document}


How can I specify a date and use that date to calculate 1, 2, 3, etc. weeks from that date?

I did not find the answer in the following documents:

And I don't understand what I am to do with Calculations with dates, although I suspect it to hold the solution I am looking for.

The simplest way is to convert the saved date with offset to a Julian number using \DTMsaveddateoffsettojulianday and then convert the result to a saved date using \DTMsavejulianday like this:

\documentclass[a4paper,12pt,]{article}

\usepackage{polyglossia}
\usepackage[british,calc]{datetime2}

\begin{document}

The present date: \DTMdate{2018-01-24}

Two weeks later: \DTMdate{2018-01-24+14}

Beautiful!

\DTMsavedate{RandomDate}{1991-02-03}

A random date \DTMusedate{RandomDate}

\newcount\julianday
\DTMsaveddateoffsettojulianday{RandomDate}{14}{\julianday}
\DTMsavejulianday{RandomDate2}{\julianday}

\DTMusedate{RandomDate2}

\end{document}


If you need to do this multiple times, it's simpler to create a helper command:

\documentclass[a4paper,12pt,]{article}

\usepackage{polyglossia}
\usepackage[british,calc]{datetime2}

% syntax: \newdateoffset{original date name}{new date name}{offset}
\newcount\julianday
\newcommand{\newdateoffset}[3]{%
\DTMsaveddateoffsettojulianday{#1}{#3}{\julianday}%
\DTMsavejulianday{#2}{\julianday}%
}

\begin{document}

The present date: \DTMdate{2018-01-24}

Two weeks later: \DTMdate{2018-01-24+14}

Beautiful!

\DTMsavedate{RandomDate}{1991-02-03}

A random date \DTMusedate{RandomDate}

\newdateoffset{RandomDate}{RandomDate2}{14}

\DTMusedate{RandomDate2}

\end{document}


And finally, since you mentioned you need to specify the initial date in the preamble, this also works and gives the same output as above:

\documentclass[a4paper,12pt,]{article}

\usepackage{polyglossia}
\usepackage[british,calc]{datetime2}

% syntax: \newdateoffset{original date name}{new date name}{offset}
\newcount\julianday
\newcommand{\newdateoffset}[3]{%
\DTMsaveddateoffsettojulianday{#1}{#3}{\julianday}%
\DTMsavejulianday{#2}{\julianday}%
}

\DTMsavedate{RandomDate}{1991-02-03}

\newdateoffset{RandomDate}{RandomDate2}{14}

\begin{document}

A random date \DTMusedate{RandomDate}

\DTMusedate{RandomDate2}

\end{document}