# mixing date formats in the same document

I am trying to make dates in my CV uniform so I thought of using the datetime2 package. The problem is that sometime I have a full date, sometimes the year and the month, and sometimes only the month. I couldn't find a way to do this with the commands provided by the package. What I would like to accomplish is something like this:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[USenglish]{babel}
\usepackage[useregional]{datetime2}
\selectlanguage{USenglish}

\begin{document}
\DTMdate{2019-1-15}
\DTMdate{2019-1} % <- error here
2019 % I don't need special command here

\end{document}


Is there a way to extend the package to accomplish this? Or is there perhaps a better package to do this properly? I also need data ranges.

Define your own function that could handle supplying only a year, or a year-and-month, or a full date (year, month and day). Below I use xparse to break an input of the form <year>-<month>-<day> into 3 separate components. A missing component receive -NoValue- which can be used in a test \IfValueTF{.}{<true>}{<false>}:

\documentclass{article}

\usepackage{xparse}
\usepackage[USenglish]{babel}
\usepackage[useregional]{datetime2}
\selectlanguage{USenglish}

\makeatletter
\NewDocumentCommand{\setdate}{> { \SplitArgument { 2 } { - } } m}{%
\@setdate#1}
\NewDocumentCommand{\@setdate}{ m m m }{%
\IfValueTF{#2}
{\IfValueTF{#3}
{\DTMdate{#1-#2-#3}}% Year, month and day
{\DTMdate{#1-#2-1}}% Year and month only
}%
{\DTMdate{#1-1-1}}% Year only
}
\makeatother

\begin{document}

\setdate{2019-1-15}

\setdate{2019-1}

\setdate{2019}

\end{document}


You can update the components above (commented) and replace it with a new function if you want to print/process the year-only, or year-and-month only outputs. For example, using

% ...
\usepackage[useregional,calc]{datetime2}
% ...

\makeatletter
\NewDocumentCommand{\setdate}{> { \SplitArgument { 2 } { - } } m}{%
\@setdate#1}
\NewDocumentCommand{\@setdate}{ m m m }{%
\IfValueTF{#2}
{\IfValueTF{#3}
{\DTMdate{#1-#2-#3}}% Year, month and day
{\DTMmonthname{#2} \number#1}% Year and month only
}%
{#1}% Year only
}
\makeatother


(note the addition of the calc component for datetime2), you get

• If you provide only year and month it should say January 2015 only. Am I missing something? – aaragon Jul 12 '19 at 18:45
• @aaragon: Yes. I mention you can define your own displays for the separate conditions. I've added something to that effect at the end of my answer. – Werner Jul 12 '19 at 19:16
• Very nice! Last question. Is there any option that we can pass so that months are abbreviated? So print Jan. instead January. – aaragon Jul 15 '19 at 11:27
• And would it be possible to easily insert a range of days? Something like \setdate{2010-05-{20-24}} to yield May 20-24, 2010 (this doesn't work). – aaragon Jul 15 '19 at 12:20
• @aaragon: Perhaps ask a follow-up question to address that. As an aside: How would you propose a date range that spans multiple days and months (or years)? – Werner Jul 15 '19 at 14:38