5

The datetime package is now obsolete and I would like to switch to the newer datetime2 pacakge, but I am having trouble recreating the behavior that I used the datetime package for. The following MWE works with the datetime package, but causes all sorts of problems with the datetime2 package.

\documentclass{article}
\usepackage{datetime}
%\usepackage{datetime2}
\computedayofweek{19}{1}{2016}
\newcommand{\mydayname}{\dayofweeknameid{\the\dayofweek}}
\newcommand{\mydaynumber}{\ordinaldate{19}}
\newcommand{\mymonthname}{\monthname[1]}
\begin{document}
\mydayname, \mydaynumber \mymonthname
\end{document}

Is it possible to redefine \mydayname, \mydaynumber, and \mymonthname (with the possible addition of code before/after the definitions), so that they produce the same results with the datetime2 package?

  • Welcome to the club! Just imagine what it is like trying to set this up for another language.... I tried and failed. I then tried to get it to work for English and found I couldn't. So I've patched up datetime for the other language and kept with that. – cfr Jan 19 '16 at 23:45
  • You can't do this. See tex.stackexchange.com/questions/261641/…. In particular see Nicola's answer. You cannot get the name of the month in a language-agnostic way. Whether you can get it at all depends on the language module. datetime2 itself provides only numeric stuff. As I say in comments there (and have just said again, though surely Nicola is sick of me doing so), datetime should not, in my opinion, be stamped 'obsolete'. – cfr Jan 19 '16 at 23:57
  • It should be marked 'unmaintained'. That is not the same. datetime2 does not provide the functionality of datetime. As a result, I just don't see how it can be said to render it 'obsolete'. It is no longer maintained. That is perfectly reasonable and fine. – cfr Jan 19 '16 at 23:59
  • @cfr I do not know your full use case, but the pgfcalendar package is pretty powerful. – StrongBad Jan 20 '16 at 1:16
  • To be honest, datetime works fine now I've substituted a patched language file. But that's an interesting suggestion: TikZ had really not occurred to me for something like this. I wonder how linguistically-savvy it is... ;). I just live in fear it may disappear. – cfr Jan 20 '16 at 1:32
3

If replication is all you're after, then the following should do it:

Tuesday, 19th January

\documentclass{article}
\usepackage[calc]{datetime2}
\usepackage{fmtcount}
\DTMsavedate{mydate}{2016-01-19}% \DTMsavenow{\mydate}
\newcommand{\mydayname}{\pgfcalendarweekdayname{\DTMfetchdow{mydate}}}
\newcommand{\mydaynumber}{\ordinalnum{\DTMfetchday{mydate}}}
\newcommand{\mymonthname}{\pgfcalendarmonthname{\DTMfetchmonth{mydate}}}
\begin{document}
\mydayname, \mydaynumber{} \mymonthname
\end{document}

datetime used to load (require) fmtcount, which is now manually loaded in order to use \ordinalnum. Additionally, in order to calculate the day-of-week values, you need to add the calc package option. This loads and gives you access to the pgfcalender module, from which one can use the expandable \pgfcalendarweekdayname and \pgfcalendarmonthname (including others, if needed).

  • If you use \pgfcalendardatetojulian and \pgfcalendarjuliantoweekday, then there is no need for the datetime2 package at all in this case. – StrongBad Jan 20 '16 at 1:14
3

You can replicate that format using one of the little endian date styles provided by datetime2-english. For example:

\documentclass{article}

\usepackage[en-GB,showdow]{datetime2}

\DTMlangsetup[en-GB]{dowdaysep={,\space},ord=raise,showyear=false}

\begin{document}
\DTMdate{2016-01-19}

\end{document}

Produces:

Tuesday, 19th January

More generally, datetime and datetime2 provide language-specific commands for use within date styles (to ensure the words match the date syntax regardless of the surrounding language) and language-sensitive commands for standalone use.

Language-Specific

The language-specific commands are defined in the dt-dialect.def files provided with datetime and in the independently installed datetime2-language modules provided for datetime2.

Month Names

(See also How do I extract a month name in datetime2?)

  • datetime

    Each file dt-dialect.def provides the command \monthnamedialect that takes an optional argument, indicating the month number. For example, with dt-british.def:

    \monthnamebritish[1]
    

    (which, in fact, uses \monthnameenglish provided by datetime-defaults.sty).

    Some languages have alternative month names, which may have been implemented with datetime. For example, dt-usorbian.def provides \monthnamenewusorbian and \monthnameoldusorbian with \monthnameusorbian set as a synonym for \monthnamenewusorbian.

  • datetime2

    Each language file datetime2-language.ldf (or datetime2-language-modifier.ldf) provides the command \DTMlanguagemonthname that takes a mandatory argument, indicating the month number. For example, with datetime2-english-base.ldf:

    \DTMenglishmonthname{1}
    

    Again, there is support for regions that have variant month names. For example, datetime2-usorbian provides \DTMusorbiannewmonthname and \DTMusorbianoldmonthname with \DTMusorbianmonthname initially defined to \DTMusorbiannewmonthname. This module provides the style option, which can take the value new or old to switch between the styles. Similarly, datetime2-serbian provides Latin and Cyrillic month names (which datetime's dt-serbian.def doesn't provide) and an alphabet option to switch between them.

Abbreviated Month Names

Some but not all language date styles provide abbreviated month names. If they are supported in datetime then they are also supported in datetime2. The reason for the lack of support is simply down to me not being fluent in every known language and not knowing the correct abbreviations. The advantage of the modular approach used by datetime2 is that each language module can be maintained by someone familiar with the language, so datetime2 modules that have been adopted may now have support added that wasn't present with datetime.

  • datetime

    Where supported, the dt-dialect.def file provides the command \shortmonthnamedialect that takes an optional argument. For example:

    \shortmonthnamebritish[1]
    
  • datetime2

    Where supported, the datetime2-language.ldf file provides the command \DTMlanguageshortmonthname that takes a mandatory argument. For example:

    \DTMenglishshortmonthname{1}
    

Day of Week Name

As with the abbreviated month name, not all language settings support the day of week name (for the same reason).

  • datetime

    Where supported, dt-dialect.def provides \dayofweeknameiddialect, with a mandatory argument, which must be a number from 1 (Sunday) to 7 (Saturday). For example

    \dayofweeknameidbritish{3}
    

    As with the month names, there may also be an abbreviated version \shortdayofweeknameiddialect.

    The day of week index can be computed from the current date using \computedayofweek{day}{month}{year} with the result stored in the register \dayofweek (as in your MWE). Date styles that implement the day of week need to use \computedayofweek, which means that they have to be robust.

  • datetime2

    Where supported, datetime2-language.ldf provides \DTMlanguageweekdayname that takes a mandatory argument, which must be in the range 0 (Monday) to 6 (Sunday). This uses a different indexing system from datetime in order to be compatible with pgfcalendar. For example

    \DTMenglishweekdayname{1}
    

    As with the month names, there may also be an abbreviated version \DTMlanguageshortweekdayname.

    Unlike datetime, the datetime2 date styles don't compute the day of week index as it should already have been computed before being passed to the style. This means that commands like \today and \DTMdisplaydate can be used in expandable contexts. The robust command \DTMdate internally computes the day of week index when parsing its argument using pgfcalendar commands through the datetime2-calc.sty interface, but the index can be explicitly computed from a date using \DTMcomputedayofweekindex. For example:

    \DTMcomputedayofweekindex{2016-01-19}{\result}
    \DTMenglishweekdayname{\result}
    

Day of Month

  • datetime

    The day of month number is formatted by the command \ordinaldatedialect provided dt-dialect.def. For example:

    \ordinaldatebritish{19}
    

    In this case, fmtcount's \ordinalnum is used, which is actually language-sensitive and is non-expandable. The suffix can be raised or level using the package options raise or level, but not all dialects recognise those options.

  • datetime2

    Each datetime2-language.ldf file provides \DTMlanguageordinal to format the day of month number. For example:

    \DTMenglishordinal{19}
    

    This doesn't use fmtcount and is expandable. Dialects that allow modifications to the ordinal suffix format usually have an ord setting. These settings are specific to the particular date style and are described in the documentation for the associated language module. For example, the en-GB style (provided by datetime2-english) has an ord setting that allows the values raise, level, omit and sc.

Complete example:

\documentclass{article}

\usepackage[en-GB,calc]{datetime2}

\DTMlangsetup[en-GB]{ord=raise}

\begin{document}

\DTMcomputedayofweekindex{2016-01-19}{\result}
\DTMenglishweekdayname{\result},
\DTMenglishordinal{19}
\DTMenglishmonthname{1}

\end{document}

Language-Sensitive Commands

There are language-sensitive commands for standalone use of particular components of the date. With datetime2, you'll need datetime2-calc.sty, which is automatically loaded through the calc or showdow package options. (Make sure you have at least v1.3 of datetime2 unless you just want to use the pgfcalendar commands). These language-sensitive commands are non-expandable for both packages.

Month Name

  • datetime

    The language-sensitive command \monthname takes an optional argument, but it's far more complicated than simply doing \monthnamedialect as it tries to ensure that the relevant regional file dt-dialect.def has been loaded. If the control sequence given by monthname\languagename doesn't exist, it falls back on \monthnameenglish, which is always defined. This command can't be used in expandable contexts. Example:

    \monthname[1]
    

    Where supported, there are analogous commands for abbreviated and first letter upper case use.

  • datetime2 (datetime2-calc.sty required)

    Since datetime2-calc automatically loads the pgfcalendar package, you can use the commands provided by that package, such as \pgfcalendarmonthname. For example:

    \pgfcalendarmonthname{1}
    

    As from v1.3, datetime2-calc also provides \DTMmonthname. As with datetime's \monthname, this is far more complicated than simply doing \DTMlanguagemonthname, but in this case you have to ensure that the relevant language module has been loaded as loading modules on-the-fly in the middle of a document can have some unpleasant side-effects. Example:

    \DTMmonthname{1}
    

    This command uses \pgfcalendarmonthname as a fallback. Where supported, there are analogous commands for abbreviated and first letter upper case use.

Day of Week Name

  • datetime

    The week day name for the current language can be obtained with the command \dayofweeknameid. As with \monthname, this performs a check to determine if the relevant language file has been loaded. If the control sequence given by dayofweeknameid\languagename exists, that's used otherwise \dayofweeknameidenglish is used as a fallback. Example:

    \dayofweeknameid{3}
    

    Again, where supported, there are analogous commands for the abbreviated or first letter upper case versions.

  • datetime2

    The week day name can be obtained through \pgfcalendarweekdayname or (datetime2 v1.3 onwards) \DTMweekdayname, which behaves in a similar way to \DTMmonthname. The pgfcalendar command is used as a fallback if the weekday name isn't supported for the current language. Example:

    \DTMweekdayname{1}
    

    Again, where supported, there are analogous commands for the abbreviated or first letter upper case versions.

Day of Month

  • datetime

    The language-sensitive day of month can be displayed using \ordinaldate. Again, this performs a check to determine if the appropriate language file has been loaded. If the control sequence given by ordinaldate\languagename is defined, then that's used otherwise the argument is simply displayed without any formatting. Example:

    \ordinaldate{19}
    
  • datetime2

    The datetime2-calc package (v1.3 onwards) provides \DTMordinal. Example:

    \DTMordinal{19}
    

    This just displays the number as a fallback if support for the current language isn't provided.

Example:

\documentclass{article}

\usepackage[en-GB,showdow]{datetime2}

\DTMlangsetup[en-GB]{ord=raise}

\begin{document}

\DTMcomputedayofweekindex{2016-01-19}{\result}
\DTMweekdayname{\result},
\DTMordinal{19}
\DTMmonthname{1}

\end{document}

Comparisons

The examples below use hyperref to illustrate the effect in bookmarks and \pagestyle{headings} to illustrated the effect in page headings. Ideally, the date should appear in the bookmark on the left and in upper case in the page heading at the top.

datetime with specific language commands

\documentclass{article}

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

\begin{document}
\tableofcontents

\pagestyle{headings}

\computedayofweek{19}{1}{2016}

\section{\dayofweeknameidbritish{\the\dayofweek}, 
\protect\ordinaldatebritish{19}
\monthnamebritish[1] (\languagename)}

\end{document}

Produces:

image of page with incorrect bookmark and header

datetime2 with specific language commands

\documentclass{article}

\usepackage[british]{babel}
\usepackage[colorlinks]{hyperref}
\usepackage[en-GB,calc]{datetime2}

\DTMlangsetup[en-GB]{ord=raise}

\begin{document}
\tableofcontents

\pagestyle{headings}

\DTMcomputedayofweekindex{2016-01-19}{\result}

\section{\DTMenglishweekdayname{\result}, \DTMenglishordinal{19} 
\DTMenglishmonthname{1} (\languagename)}

\end{document}

Produces (best result):

image of page with correct bookmarks and headings

datetime with language-sensitive commands

\documentclass{article}

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

\begin{document}
\tableofcontents

\pagestyle{headings}

\computedayofweek{19}{1}{2016}

\section{\dayofweeknameid{\the\dayofweek}, \ordinaldate{19}
\monthname[1] (\languagename)}

\end{document}

Produces:

image of page with incorrect bookmark and header

datetime2 with language-sensitive commands

\documentclass{article}

\usepackage[british]{babel}
\usepackage[colorlinks]{hyperref}
\usepackage[en-GB,calc]{datetime2}

\DTMlangsetup[en-GB]{ord=raise}

\begin{document}
\tableofcontents

\pagestyle{headings}

\DTMcomputedayofweekindex{2016-01-19}{\result}

\section{\DTMweekdayname{\result}, \DTMordinal{19} \DTMmonthname{1}
(\languagename)}

\end{document}

Produces

image of page with incorrect bookmarks and headers

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.