How do I extract a month name in datetime2?

You can do this for a particular language or variant if the relevant module supports it, using the commands provided by that module. Without a complete example, I can't say whether this is possible for your situation or not because I don't have a clue what your preamble is.

For British English, for example, I can do this:

\documentclass[british]{article}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage{datetime2}
\DTMsetup{useregional}

\begin{document}
\DTMsavenow{thisone}
\DTMenglishmonthname{\DTMfetchmonth{thisone}}
\end{document}

fancy way to typeset current month

But \DTMenglishmonthname is obviously specific to the various variants of English supported by datetime2-english. Another language module may not provide a similar facility - it just depends on the particular module.

Something analogous works for Welsh, although the particular command obviously differs:

\documentclass[welsh]{article}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage{datetime2}
\DTMsetup{useregional}

\begin{document}
\DTMsavenow{thisone}
\DTMwelshmonthname{\DTMfetchmonth{thisone}}
\end{document}

Awst

There doesn't seem to be a language-agnostic user-level macro for retrieving the name of the month, though. But perhaps there is and I'm just not seeing it - as I say, I've failed miserably in my attempt to transition to datetime2 and am, therefore, still using datetime.


I've updated this answer following the release of datetime2 v1.3.

  1. Each language module provides a command in the form \DTMlangmonthname where lang is the root language name (e.g. \DTMfrenchmonthname provided by datetime2-french or \DTMenglishmonthname provided by datetime2-english). This command has a required argument and may be used in expandable contexts. It's analogous to datetime's \monthnamelanguage commands (e.g. \monthnamefrench provided by dt-french.def or \monthnameenglish provided by datetime-defaults.sty). The datetime version can't be used in expandable contexts and has an optional argument.

    Some of the language modules may additionally provide alternative commands. For example, the datetime2-usorbian module provides \DTMusorbiannewmonthname and \DTMusorbianoldmonthname, and the datetime2-serbian module provides \DTMserbiancyrmonthname and \DTMserbianlatinmonthname. In these cases, \DTMlangmonthname is set to whatever command is the default. This can be changed through the language module options (such as the style key for the usorbian module or the alphabet key for the serbian module).

Example:

\documentclass{article}

\usepackage[french,british]{babel}
\usepackage{datetime2}
\usepackage[colorlinks]{hyperref}

\pagestyle{headings}

\begin{document}

\tableofcontents

\clearpage

\section{\DTMenglishmonthname{1} (English)}

\section{\DTMfrenchmonthname{1} (French)}

\end{document}

This produces

image of table of contents

on the first page, and

image of second page

on the second page. The PDF bookmarks appear correctly:

image of bookmarks

For comparison, the analogous datetime code is:

\documentclass{article}

\usepackage[french,british]{babel}
\usepackage{datetime}
\usepackage[colorlinks]{hyperref}

\pagestyle{headings}

\begin{document}

\tableofcontents

\clearpage

\section{\monthnameenglish[1] (English)}

\section{\monthnamefrench[1] (French)}

\end{document}

The first page looks the same as the datetime2 example, but the second page is incorrect as the month name hasn't been converted to upper case in the header:

image of second page

The PDF bookmarks are also incorrect:

image of PDF bookmarks

  1. Since datetime2-calc automatically loads the pgfcalendar package, you can use the month name commands provided by that package, so \pgfcalendarmonthname is the nearest equivalent to datetime's \monthname command, but it requires the translator package.

Modifying the above example:

\documentclass[french,british]{article}

\usepackage{babel}
\usepackage{translator}
\usepackage[calc]{datetime2}
\usepackage[colorlinks]{hyperref}

\pagestyle{headings}

\begin{document}

\tableofcontents

\clearpage

\selectlanguage{british}
\section{\pgfcalendarmonthname{1} (English)}

\selectlanguage{french}
\section{\pgfcalendarmonthname{1} (French)}

\end{document}

This produces

image of table of contents

on the first page and

image of second page

on the second page, but the PDF bookmarks are incorrect:

image of PDF bookmarks

This produces a warning from hyperref because it can't process \translate.

The equivalent datetime version of this example is

\documentclass[french,british]{article}

\usepackage{babel}
\usepackage{datetime}
\usepackage[colorlinks]{hyperref}

\pagestyle{headings}

\begin{document}

\tableofcontents

\clearpage

\selectlanguage{british}
\section{\monthname[1] (English)}

\selectlanguage{french}
\section{\monthname[1] (French)}

\end{document}

This produces

image of table of contents

on the first page and

image of second page

on the second page. Again the page header is incorrect as \monthname can't be converted to upper case. The PDF bookmarks are also incorrect:

image of PDF bookmarks

  1. As from version 1.3, the datetime2-calc package now provides \DTMmonthname, which is the closest match to datetime's \monthname command. This new command is robust and works as follows: it tries to determine if \DTMlangmonthname exists, first with lang set to \languagename and, if that fails, with lang set to \TrackedLanguageFromDialect{\languagename}. If the command \DTMlangmonthname command exists, it's used. If not, the \pgfcalendarmonthname command is used instead with a warning. (If this occurs, it most likely means the appropriate datetime2 language module hasn't been installed or loaded.)

Adapting the previous example from datetime to datetime2:

\documentclass[french,british]{article}

\usepackage{babel}
\usepackage[calc]{datetime2}
\usepackage[colorlinks]{hyperref}

\pagestyle{headings}

\begin{document}

\tableofcontents

\clearpage

\selectlanguage{british}
\section{\DTMmonthname{1} (English)}

\selectlanguage{french}
\section{\DTMmonthname{1} (French)}

\end{document}

This pretty much produces the same result as the previous datetime example. The only difference is in the PDF bookmarks, which are again incorrect, but just incorrect in a slightly different way:

image of PDF bookmarks

So the closest match to \monthname[n] is \DTMmonthname{n}, which has all the same drawbacks as the original datetime command.


Original Answer

I deliberately wanted to avoid a general month name macro in datetime2 because it caused problems with datetime when users used a style with the syntax of one language (overriding babel's date switching mechanism) but the month name from the language currently in use (because the style used \monthname). The base datetime2 package only deals with numeric styles, and my intention was to ensure that all language/region settings were dealt with by the language modules. (The showdow option is an exception to this, see How does showdow work in datetime2?) There were three main points I particularly wanted to address:

  1. Prevent accidental mixing of syntax and translated fixed names.
  2. Allow the language modules flexibility when implementing month names, as some languages have alternative month names (as in a recent question) or alternative styles. Perhaps a language module might provide "new style" and "old style" date formats. This kind of variation makes a simple \monthname mapping much more complicated and less intuitive.
  3. Ensure date styles are fully expandable (as much as possible) to allow for use in PDF bookmarks. What should happen if a language module hasn't been loaded and you tried to use \csname DTM\languagename monthname? Silently ignore it (and confuse users) or issue a warning or error (in which case this could cause more obscure errors in certain contexts).

However, since the language modules provide the settings showdayofmonth and showyear, you can fetch just the month name by setting both to false. For example:

\documentclass[french]{article}

\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[warn=false,useregional]{datetime2}

\DTMlangsetup{showdayofmonth=false,showyear=false}

\begin{document}

\edef\thismonth{\today}
\show\thismonth

\end{document}

This also works with polyglossia:

\documentclass[french]{article}

\usepackage{polyglossia}
\usepackage[warn=false,useregional]{datetime2}

\DTMlangsetup{showdayofmonth=false,showyear=false}

\begin{document}

\edef\thismonth{\today}
\show\thismonth

\end{document}

Edit:

There's another possible solution:

\documentclass[french]{article}

\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{translator}
\usepackage[calc]{datetime2}

\begin{document}

\pgfcalendarmonthname{\month}

\end{document}

This is because datetime2-calc loads pgfcalendar, but I don't know if it has support for polyglossia.


In my opinion, the datetime2 package should add an interface to the \DTM<language>monthname macros (and for the day of week macros as well). Here is an attempt:

\documentclass{article}
\usepackage[ngerman,english]{babel}
\usepackage[ngerman,english]{datetime2}

\addto\extrasenglish{\def\DTMlanguagename{english}}
\addto\extrasngerman{\def\DTMlanguagename{german}}
\newcommand{\DTMmonthname}{\csname DTM\DTMlanguagename monthname\endcsname}

\DTMsavedate{bach}{1685-3-21}

\begin{document}

J. S. Bach was born in \DTMmonthname{\DTMfetchmonth{bach}}.

\selectlanguage{ngerman}

J. S. Bach wurde im \DTMmonthname{\DTMfetchmonth{bach}} geboren.

\end{document}

enter image description here

Tags:

Datetime2