1

I frequently use the \formatdate{01}{02}{2016} command to get a result like 1st February, 2016

Now I need the same thing, but without the day. I just need the month and year. Unfortunately, the \formatdate command only accepts 3 parameters.

Is there a command that does the above but only with month and year?

3

It's better to switch to the new datetime2 package, which replaces datetime. This example also requires datetime2-english as the datetime2 language modules are distributed separately.

\documentclass{article}

\usepackage[en-GB]{datetime2}
\DTMlangsetup[en-GB]{showdayofmonth=false,monthyearsep={,\space}}

\begin{document}
\DTMdate{2016-02-01}. % not expandable

\DTMdisplaydate{2016}{2}{1}{-1}. % expandable

\end{document}

February, 2016. February, 2016.

You can localise \DTMlangsetup:

\documentclass{article}

\usepackage[en-GB]{datetime2}
\DTMlangsetup[en-GB]{monthyearsep={,\space}}% global

\begin{document}
\section{Before}

Just using global setting.

\DTMdate{2016-02-01}.

\DTMdisplaydate{2016}{2}{1}{-1}.

\today.

\section{Scoped}

Localising one of the settings. The non-conflicting global settings
are still in place.

{% scope
  \DTMlangsetup[en-GB]{showdayofmonth=false}% local
  \DTMdate{2016-02-01}.

  \DTMdisplaydate{2016}{2}{1}{-1}.

  \today.
}

\section{After}

Just using global setting.

\DTMdate{2016-02-01}.

\DTMdisplaydate{2016}{2}{1}{-1}.

\today.
\end{document}

Section 1 Before. Just using global setting. 1st February, 2016. 1st February, 2016. 22nd April, 2018. Section 2 Scoped. Localising one of the settings. The non-conflicting global settings are still in place. February, 2016. February, 2016. April, 2018. Section 3 After. Just using global setting. 1st February, 2016. 1st February, 2016. 22nd April, 2018.

\DTMdate has a more convenient interface and is able to compute the day of week if the calc package option is used (which makes datetime2.sty automatically load datetime2-calc.sty), but it's robust and can't be used in an expandable context, such as writing information to an external file (e.g. the table of contents or bookmarks).

For example, if \DTMdate{2016-02-01} is used in a sectioning command (such as \chapter or \section) it will be written to the .toc file as \DTMdate{2016-02-01} not as February, 2016 (or whatever the style format is at that point). This means that the date will appear in the table of contents in the style currently in effect when the .toc file is read by \tableofcontents whereas the section heading will use whatever style is in effect at that point.

Robust non-expandable material also can't be used in PDF bookmarks. The command will be discarded and you'll be left with just the argument.

For example:

\documentclass{article}

\usepackage[en-GB]{datetime2}
\usepackage[hidelinks]{hyperref}
\DTMlangsetup[en-GB]{monthyearsep={,\space}}% global

\begin{document}
\tableofcontents

\DTMlangsetup[en-GB]{showdayofmonth=false}
\section{\DTMdate{2016-02-01}}

\end{document}

This triggers the warning:

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `\DTMdate' on input line 11.

image of document in PDF viewer

The bookmark shows the section as "2016-02-01", the table of contents shows the section as "1st February, 2016" and the section header shows as "February, 2016".

If \DTMdisplaydate is used instead, the result is more consistent:

\documentclass{article}

\usepackage[en-GB]{datetime2}
\usepackage[hidelinks]{hyperref}
\DTMlangsetup[en-GB]{monthyearsep={,\space}}% global

\begin{document}
\tableofcontents

\DTMlangsetup[en-GB]{showdayofmonth=false}
\section{\DTMdisplaydate{2016}{02}{01}{-1}}

\end{document}

image of document in PDF viewer

The bookmarks, table of contents and section now all show the same "February, 2016", which is the style in effect when \section was used.

  • Thanks for your suggestion using datetime. If I want to show the full date somewhere, how would I do that? Do I have to use \DTMlangsetup over and over again? – Socrates Apr 22 '18 at 15:18
  • Also, what is the difference between expandable and not expandable? – Socrates Apr 22 '18 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.