[Tex/LaTex] Create custom date command using numbers

datetimeisodate

Is there any way to create a custom command that prints the date (using the language specified in babel to be possible) from a given ISO 8601-formatted date?

What I would like is that

\today
\customdate{160130} % or \customdate{20160130}

both print

enter image description here

Best Answer

The datetime2 package is designed to work with language packages, such as babel and polyglossia, but you need to additionally install the relevant language module. For example, datetime2-english or datetime2-french. The numeric style is the default, but you can use the package option useregional to allow the language change to automatically change the date style.

Example:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[french,british]{babel}
\usepackage[useregional]{datetime2}

\begin{document}
\selectlanguage{british}

\DTMdate{2016-01-30}.

\selectlanguage{french}

\DTMdate{2016-01-30}.

\end{document}

Produces:

30th January 2016. 30 janvier 2016

Edit:

The date styles are designed to be expandable so commands like \today can be used in contexts where information is written to an external file (such as table of contents or bookmarks). \DTMdate is robust which means it can't be expanded but it doesn't need protecting (with \protect). There's an expandable alternative \DTMdisplaydate but it has a different syntax, which doesn't fit your requirement. These differences can be illustrated with the following examples:

\DTMdate:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[french,british]{babel}
\usepackage[useregional]{datetime2}
\usepackage[hidelinks]{hyperref}

\pagestyle{headings}

\begin{document}
\tableofcontents
\newpage

\selectlanguage{british}

\section{\DTMdate{2016-01-30} example section}

\selectlanguage{french}

\section{\DTMdate{2016-01-30} example section}

\end{document}

The table of contents looks fine with this:

image of table of contents

The page header can't convert the date to upper case (because it's robust) and it's also using the wrong language:

image of page 2

The PDF bookmarks can't process robust commands, so the bookmarks use the numeric form given in the argument of \DTMdate:

2016-01-30 example section 2016-01-30 example section

Now modifying the example to use \today:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[french,british]{babel}
\usepackage[useregional]{datetime2}
\usepackage[hidelinks]{hyperref}

\pagestyle{headings}

\begin{document}
\tableofcontents
\newpage

\selectlanguage{british}

\section{\today\ example section}

\selectlanguage{french}

\section{\today\ example section}

\end{document}

The contents page is the same as before, but the header on the next page is now correct (both language and case):

image of page 2

The PDF bookmarks are now fine:

image of bookmarks

The expandable version of \DTMdate{2016-01-30} is \DTMdisplaydate{2016}{01}{30}{-1} (or \DTMdisplaydate{2016}{01}{30}{5}) and will work in the same way as \today.

An alternative is to first store the date with \DTMsavedate and later use it with \DTMusedate, which is also expandable:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[french,british]{babel}
\usepackage[useregional]{datetime2}
\usepackage[hidelinks]{hyperref}

\DTMsavedate{mydate}{2016-01-30}

\pagestyle{headings}

\begin{document}
\tableofcontents
\newpage

\selectlanguage{british}

\section{\DTMusedate{mydate} example section}

\selectlanguage{french}

\section{\DTMusedate{mydate} example section}

\end{document}

This produces the same as the earlier example with \today. The page headers and PDF bookmarks are correct.