2

I want to be able to just type either the short version or long version by a command, like \longtoday and \shorttoday. Some more examples of short dates are 12 May 17 and 01 Jan 07, long dates are the same, except written out months and years, 12 May 2017 and 01 January 2007.

I was thinking of using isodate, but I can't figure out how it works quite that well. Can anyone help? Thank you!

4

You can do it with datetime2.

\documentclass{article}
\usepackage{datetime2}

\DTMusemodule{english}{en-GB}
\DTMnewdatestyle{long}{%
  \renewcommand{\DTMdisplaydate}[4]{%
    \number##3~\DTMenglishmonthname{##2}~\number##1\relax
  }%
  \renewcommand{\DTMDisplaydate}{\DTMdisplaydate}%
}
\DTMnewdatestyle{short}{%
  \renewcommand{\DTMdisplaydate}[4]{%
    \number##3~\DTMenglishshortmonthname{##2}~\number##1\relax
  }%
  \renewcommand{\DTMDisplaydate}{\DTMdisplaydate}%
}
\newcommand{\longtoday}{{\DTMsetdatestyle{long}\today}}
\newcommand{\shorttoday}{{\DTMsetdatestyle{short}\today}}

\begin{document}

\longtoday

\shorttoday

\end{document}

enter image description here

3

Another (but shorter) method with datetime2:

\documentclass{article}
\usepackage[en-GB]{datetime2}

\newcommand{\longtoday}{{\DTMlangsetup[en-GB]{abbr=false,ord=omit}\today}}
\newcommand{\shorttoday}{{\DTMlangsetup[en-GB]{abbr,ord=omit}\today}}

\begin{document}

\longtoday

\shorttoday

\end{document}

13 December 2017 13 Dec 2017

If the date needs to appear in the PDF bookmarks, then you need to set the style before the sectioning command. For example:

\documentclass{article}
\usepackage[en-GB]{datetime2}
\usepackage{hyperref}

% default setting:
\DTMlangsetup[en-GB]{abbr=false,ord=omit}

\begin{document}
\DTMlangsetup[en-GB]{abbr}
\section{\today}

\DTMlangsetup[en-GB]{abbr=false}
\section{\today}

\end{document}

image of PDF showing bookmarks

Similarly for any other expandable context, such as writing the date to an external file.

If you want to ensure two-digit day of month (without an ordinal suffix):

\renewcommand*{\DTMenglishordinal}[1]{\DTMtwodigits{#1}}
2

The following should do.

\documentclass[]{article}

\newcommand\defdate[3]{% Just for testing purpose
  \day=#1\relax
  \month=#2\relax
  \year=#3\relax
  \ignorespaces}

\def\JayLaoneShortyear#1#2#3\relax{%
  #3}
\def\JayLaoneDayTWODIGITS#1#2\relax{%
  \if\relax\detokenize{#2}\relax
    0#1%
  \else
    #1#2%
  \fi}

\newcommand*{\shorttoday}{%
  \expandafter\JayLaoneDayTWODIGITS\number\day\relax\space
  \ifcase\month
    \or Jan%
    \or Feb%
    \or Mar%
    \or Apr%
    \or May%
    \or Jun%
    \or Jul%
    \or Aug%
    \or Sep%
    \or Oct%
    \or Nov%
    \or Dec\fi
  \space
  \expandafter\JayLaoneShortyear\number\year\relax
}
\newcommand*{\longtoday}{%
  \expandafter\JayLaoneDayTWODIGITS\number\day\relax\space
  \ifcase\month
    \or January%
    \or February%
    \or March%
    \or April%
    \or May%
    \or June%
    \or July%
    \or August%
    \or September%
    \or October%
    \or November%
    \or December\fi
    \space
    \number\year
}

\begin{document}
\noindent
\defdate{1}{12}{2018}
\shorttoday\\
\longtoday\\
\defdate{7}{1}{2010}
\shorttoday\\
\longtoday\\
\defdate{17}{1}{2009}
\shorttoday\\
\longtoday
\end{document}

enter image description here

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.