The strategy here has several components to develop \datecommand
, as requested by the OP. First, I had to examine which of three different branches to use:
sharing a common month and year (\dayrange
)
sharing a common year (\monthrange
)
spanning beyond a year's boundary (\yearrange
}
Then, for branches 1 and 2, I had to develop special routines to override the checks and behavior employed by datetime2
. In the end, I found that a routine \DTMifbool
, with four arguments, was used to typeset the various pieces. Therefore, I had to selectively replace this macro with an alternative, when needed.
In case 1, I had to compare argument #2
to the string showdayofmonth
. If I found a match, I replaced \DTMifbool
with \altADTMifbool
, which did no checks on day's format, so that I could replace the day with a range like 1--12
. The replacement also prevented the day's suffix like st
, nd
and rd
from being printed.
In case 2, I had to compare argument #2
to the string showyear
. If I found a match, I replaced DTMifbool
with \altBDTMifbool
, which basically shut down the call altogether, so that no separator nor year were printed (example de 2012
). I then restored \DTMifbool
and finished up with -- <closing date>
/
In case 3, I simply typeset <opening date> -- <closing date>
.
In the MWE, I show the three branches, each for british
, french
, and portuges
language styles.
\documentclass{article}
\usepackage[french,british,portuges]{babel}
\usepackage[useregional,calc]{datetime2}
%%%%%%%%%%%%% WORKS FOR LUA + XELATEX + PDFLATEX ENGINES
%\usepackage{pdftexcmds}
%\makeatletter
%\let\strcmp\pdf@strcmp
%\makeatother
%%%%%%%%%%%%% WORKS ONLY FOR PDFLATEX AND XELATEX ENGINES
\let\strcmp\pdfstrcmp
%%%%%%%%%%%%% END
\DTMusemodule{british}{en-GB}
\DTMusemodule{french}{french}
\DTMusemodule{portuges}{portuges}
\newcommand\datecommand[1]{\dcaux(#1)}
\def\dcaux(#1-#2-#3/#4-#5-#6){%
\ifnum\strcmp{#1}{#4}=0\relax
\ifnum\strcmp{#2}{#5}=0\relax
\dayrange{#1}{#2}{#3}{#6}%
\else
\monthrange{#1}{#2}{#3}{#5}{#6}%
\fi
\else
\yearrange{#1}{#2}{#3}{#4}{#5}{#6}%
\fi
}
\let\svDTMifbool\DTMifbool
\newcommand\altADTMifbool[4]{%
\ifnum\strcmp{#2}{showdayofmonth}=0\relax
\expandafter\noparsedayofmonth
\else
\svDTMifbool{#1}{#2}{#3}{#4}%
\fi
}
\newcommand\altBDTMifbool[4]{%
\ifnum\strcmp{#2}{showyear}=0\relax
\else
\svDTMifbool{#1}{#2}{#3}{#4}%
\fi
}
\newcommand\dayrange[4]{%
\begingroup
\let\DTMifbool\altADTMifbool
\def\noparsedayofmonth{\number#3--\number#4\ }%
\DTMdate{#1-#2-1}
\endgroup
}
\newcommand\monthrange[5]{%
\begingroup
\let\DTMifbool\altBDTMifbool
\DTMdate{#1-#2-#3}
\endgroup
-- \DTMdate{#1-#4-#5}%
}
\newcommand\yearrange[6]{%
\DTMdate{#1-#2-#3} -- \DTMdate{#4-#5-#6}%
}
\begin{document}
\newcommand\branchtests{%
\par\datecommand{2012-10-01/2012-10-12}
\par\datecommand{2012-10-01/2012-11-3}
\par\datecommand{2012-10-01/2013-1-4}\par}
\selectlanguage{british}\branchtests
\hrulefill\selectlanguage{french}\branchtests
\hrulefill\selectlanguage{portuges}\branchtests
\end{document}
