10

In datetime, there was a macro which provided the long name of the month, so if I have saved a date as, say, gitdate, I can extract the month name by:

\monthname{\getdatemonth{gitdate}}

Is there something similar in datetime2? I couldn't find anything in the manual, so I tried

\DTMfetchmonthname{gitdate}

but that was a bit optimistic.

Of course, I appreciate that datetime2 brings a whole lot more rigour to the story, and that international month names can be a bit stress-inducing, but I live in hope.


The two answers so far (@cfr and @egreg, in alphabetical order) are excellent(+1), but don't entirely solve my problem. babel (or my preference, polyglossia) seems like an awful lot of baggage to carry around for a simple monolingual document. It would be "nicer" if there were, in the absence of these packages, a "sensible default".


I can confirm that @cfr's solution using babel works correctly.

Unfortunately, substituting polyglossia for babel appears to fail.

  • 1
    A complete example would be best here. The language modules define additional macros so what works for one language - or one variant of one language - may not work for another. Personally, I'm still using datetime with a fixed language file. I can't figure out how to transition to datetime2 ;). – cfr Aug 16 '15 at 13:40
  • @cfr I'm sorry, I don't think I can improve on that too much. I'll update the question to clarify a bit, but I really haven't a clue. – Brent.Longborough Aug 16 '15 at 13:47
  • The thing is, a complete example would make it possible to say for sure whether you can do this or not. I've answered with a couple of examples but you'll need to investigate the module(s) you are using to see if something analogous would work for you. – cfr Aug 16 '15 at 14:05

3 Answers3

9

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.

cfr
  • 198,882
  • I see nowhere that an option such as british or en-UK sets in any way a string representing the current language so that one can dynamically define \DTMmonthname in such a way it uses \DTM<language>monthname. On the other hand, if you use the interface to babel, doing \def\DTMmonthname{\csname DTM\languagename monthname\endcsname} might work. – egreg Aug 16 '15 at 14:27
  • @egreg Even if you can, there's no guarantee that the language module will provide \DTM<language>monthname as far as I can tell, and the package doesn't provide any fallback, I don't think, so the command might just be undefined. – cfr Aug 16 '15 at 16:12
7

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.

Nicola Talbot
  • 41,153
  • In this case, I really don't think datetime should be declared obsolete. 'Obsolete' is used for packages whose functionality is provided by newer packages and where those newer packages are clearly preferable. datetime is the only package I'm aware of which provides the functionality it does. I appreciate the fact that it became a nightmare to maintain. But it is perfectly possible to declare a package unmaintained without claiming it is obsolete. In that case, other people could provide updates or fixes. I'm using a corrected language file but I'm blocked from sharing those corrections. – cfr Nov 11 '15 at 02:32
  • I did, by the way, make a genuine effort to switch to datetime2. I failed utterly because I need Welsh and I could not get that working. Obviously this is just due to my limitations but, on the grounds that I'm probably not the only one to suffer from such limitations, it would be nice if there was at least a datetime option available for Welsh. Right now the translations in both datetime and datetime2 are, as you know, erroneous. (They are possibly a mix of Welsh and Breton - they are certainly a mix of Welsh and something foreign.) – cfr Nov 11 '15 at 02:36
  • But this answer makes me suspect that datetime2 wouldn't work for me as a replacement anyway, even if I could figure it out. That's perfectly reasonable, but it convinces me that datetime is not obsolete, regardless of the stamp embossed on its CTAN page. – cfr Nov 11 '15 at 02:38
  • @cfr If someone wants to take over maintenance of datetime they can contact me and I'll be happy for them to resurrect it. I just have far too many free projects burdening me to spend time supporting one that has so many issues which I can't address without breaking backward compatibility. – Nicola Talbot Nov 11 '15 at 11:16
  • You can disown it without declaring it obsolete. Obsolete makes it a candidate for removal from distributions because it is assumed the functionality is provided by another package. Just ceasing to maintain it doesn't do that. I don't really see the objection to this. But it is not my package, after all. – cfr Jan 19 '16 at 23:54
6

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

egreg
  • 1,121,712