# latex date calculations with datetime2 and ddmm format

I've been trying to to display dates in the format "dd"/"mm" (months preferably roman letters) - however I can't find a single style that supports this requirement. Additionally I'm trying to calculate week long intervals with the created commands:

\documentclass[12pt]{book}
\usepackage[british,calc,useregional=numeric]{datetime2}

\newcount\myct
\newcount\datecount
\newcommand{\myday}[1]{%
\DTMsavenow{mydate}
\DTMsaveddateoffsetjulianday{mydate}{#1}{\myct}
\DTMsavejulianday{mydate}{\number\myct}
\DTMusedate{mydate}
}


And use it in the following way:

\begin{document}

\myday{1} - \myday{7}
\myday{8} - \myday{14}

\end{document}


However no matter how I try to redefine the DTMusedate commands to only display day and month I usually get some interpreter error which isn't of much use (I'm a newb at tex programming). So I'd appreciate any input.

Thanks, drinker

The datetime2 manual has an example of how to add custom date formats (page 33). Adapting from there we can make a version with dd/mm and another one with dd/roman month (note that this is not padded to two digits, because there is no zero in roman). The four arguments to \DTMdisplaydate are 1. year, 2. month, 3. day, and 4. day of week, so in this case you need arguments 3 and 2. Note that the separator / is given as a package option, you can alternatively use \DTMsetup{datesep={/}} or specify the character directly in the style definition.

MWE:

\documentclass[12pt]{article}
\usepackage[british,calc,useregional=numeric,datesep=/]{datetime2}

\DTMnewdatestyle{ddmm}{%
\renewcommand*{\DTMdisplaydate}[4]{%
\DTMtwodigits{##3}\DTMsep{yearmonth}\DTMtwodigits{##2}}%
\renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}%
}

\newcount\myct
\newcommand{\myday}[1]{%
\DTMsavenow{mydate}%
\DTMsaveddateoffsettojulianday{mydate}{#1}{\myct}%
\DTMsavejulianday{mydate}{\number\myct}%
\DTMusedate{mydate}%
}

% https://tex.stackexchange.com/questions/23487/how-can-i-get-roman-numerals-in-text
\newcommand{\RNum}[1]{\uppercase\expandafter{\romannumeral #1\relax}}

\DTMnewdatestyle{ddmmroman}{%
\renewcommand*{\DTMdisplaydate}[4]{%
\DTMtwodigits{##3}\DTMsep{yearmonth}\RNum{##2}}%
\renewcommand*{\DTMDisplaydate}{\DTMdisplaydate}%
}

\begin{document}
\section{Regular month numbers}
\DTMsetdatestyle{ddmm}
\noindent Now: \DTMsavenow{mydate}\DTMusedate{mydate}\\
\myday{0} -- \myday{7}\\
\myday{8} -- \myday{15}\\

\section{Roman month numbers}
\DTMsetdatestyle{ddmmroman}
\noindent Now: \DTMsavenow{mydate}\DTMusedate{mydate}\\
\myday{0} -- \myday{7}\\
\myday{8} -- \myday{15}\\

\end{document}


Result:

• Thanks! I've tried this since it looked promising but I got an compilation error: "! Missing control sequence inserted. <inserted text> \inaccessible l.75 \DTMsetdatestyle{ddmmroman}" - same result for the ddmm style. – drinker Oct 2 '18 at 22:51
• @drinker which version of datetime2 do you use? You can check by putting \listfiles at the line below \documentclass and looking at the terminal output. I use version 1.5.2 (2016/07/12) which compiles the MWE correctly. – Marijn Oct 3 '18 at 7:45
• Checked: it's 2016/07/12 v 1.5.2 - same as yours so I guess that's not the real issue here. I was wondering about the scope of definitions in latex and the order of definitions: does it have to be specific? Do I have to define before I use something? Can I use a style defined in preamble inside the document environment? Otherwise no clue why the new style would be unrecognized... – drinker Oct 3 '18 at 19:14
• @drinker did you try the exact MWE as I posted above? Or did you modify your real document? If the error occurs in the real document it could be caused by various other things, such as other packages that interfere with the definitions of datetime2. In that case you can try to gradually expand the MWE posted in my answer to recreate your real document step by step, and check when the error occurs. If you tried the MWE as it is and you still get errors then something else is wrong, in that case it would be helpful to see the exact error message and relevant sections of the log file. – Marijn Oct 4 '18 at 7:41
• indeed that was my concern too so I started removing packages until I ended up with, what I belived was, a minimal required version - also commenting out everything that wasn't related to your example but that didn't help. I will post my final result a bit later – drinker Oct 4 '18 at 8:36