15

How can I insert two digit year string?

I know that the command \the\year inserts four digit year string. I tried using \twodigit\year, but it also returns four digits.

I guess I need some kind of macro, but I don't know how to write one. I tried like this:

\newcommand{\shortyear}[4]{
\def\@shortyear{#3#4}}

\shortyear[\the\year]  % Empty string

But it returns empty string. The idea was to pass four digits as argument and return only the last two.

  • 1
    \twodigit is used to guarantee that a number is shown with at least two digits, not at most ;-) – user31729 Oct 8 '14 at 19:07
18

Of course, it is incumbent on the user to make sure the argument is/produces 4 digits.

\documentclass{article}
\def\shortyear#1{\expandafter\shortyearhelper#1}
\def\shortyearhelper#1#2#3#4{#3#4}
\begin{document}
\shortyear{\the\year}\par
\shortyear{2036}
\end{document}

enter image description here

|improve this answer|||||
  • How could I export \shortyear and \shortyearhelper definitions to my style.sty file? I tried simply copying the commands, but the behaviour changes. – niekas Oct 8 '14 at 19:12
  • @niekas Copying should suffice, unless you have defined \shortyear elsewhere in your code, using an alternate definition. – Steven B. Segletes Oct 8 '14 at 19:14
  • Yes, you are right! Thank you very much once again! – niekas Oct 8 '14 at 19:16
  • @niekas If you need it to handle dates less than 1000 or greater than 9999, that can be done, but of course takes more code. – Steven B. Segletes Oct 8 '14 at 19:21
10

A simpler solution:

\documentclass{article}

\makeatletter
\newcommand*\short[1]{\expandafter\@gobbletwo\number\numexpr#1\relax}
\makeatother

\begin{document}

\short{\year}

\short{2014}     

\end{document}

enter image description here

In case of numeric argument, you're better using use a four digit number.

|improve this answer|||||
  • I think, that the second \makeatletter should be replaced by \makeatother, though I don't know what these commands do ;) Simply recognized the command pattern. – niekas Oct 9 '14 at 3:58
  • @niekas Yes, it should. – egreg Oct 9 '14 at 7:45
  • Your solution seems to be simple. But it could be more simple (without using eTeX and without catcode manipulation) if you say: \def\short#1{\csname @gobbletwo\expandafter\endcsname\number#1\relax} – wipet Oct 9 '14 at 13:18
  • @wipet The \relax wouldn't make it fully expandable. I see no catcode manipulation in my answer, just access to “private” macros. – egreg Oct 9 '14 at 13:23
  • 1
    The \relax was my mistake. It isn't needed. So: \def\short#1{\csname @gobbletwo\expandafter\endcsname\number#1}. – wipet Oct 9 '14 at 18:38
6

A version using the \MODULO command from the calculator package. It will not work for too large numbers, however (and too short ones ;-))

Note: The calculator package uses length registers internally, it's restricted to values from 0 to 16383, so don't use this code for years beyond 16383 ;-)

\documentclass{article}

\usepackage{calculator}%

\usepackage{datetime}%

\newcommand{\shortyear}[1]{%
  \gdef\ModuloResult{}%
  \MODULO{\twodigit{#1}}{100}{\ModuloResult}%
  \twodigit{\ModuloResult}%
}

\begin{document}
\shortyear{\year}%

\shortyear{1900}%
\end{document}%

enter image description here

Improved version -- showing the two digits representation of numbers from 1 to 16383

\documentclass{article}

\usepackage{calculator}%

\usepackage{datetime}%
\usepackage{longtable}
\usepackage{forloop}%

\newcommand{\shortyear}[1]{%
  \gdef\ModuloResult{}%
  \MODULO{\twodigit{#1}}{100}{\ModuloResult}%
  \twodigit{\ModuloResult}%
}

\newcounter{loopcounter}
\begin{document}
\begin{longtable}{ll}
  Year & Short year \tabularnewline
\endfirsthead
\forloop{loopcounter}{1}{\number\value{loopcounter} < 16384}{%
  \theloopcounter & \shortyear{\value{loopcounter}} \tabularnewline
}%
\end{longtable}


\end{document}
|improve this answer|||||
6

You can also xstring for this quite easily:

Sample output

\documentclass{article}

\usepackage{xstring}
\newcommand*{\shortyear}[1]{\StrRight{0000\number\numexpr#1}{2}\relax}

\begin{document}

\shortyear{2011}
\shortyear{5}

\shortyear{\year}

\end{document}

The final \relax is there to guarantee the expect spacing after the macro.

|improve this answer|||||
6

I give another solution to this collection of answers. No packages are needed, but eTeX primitives are used.

\def\short#1{\ifnum\shortA{#1}<10 0\fi \number\shortA{#1}}
\def\shortA#1{\numexpr #1-((#1-50)/100)*100\relax}

\short\year;  \short{9};  \short{1849});  \short{1989};  \short{123456}. 
%        14;         09;            49;             89;              56.
|improve this answer|||||
3

Simple answer: With xtrings is as simple as \StrGobbleLeft{\the\year}{2}.

Not so simple answer: In the example below, the \sy ("short year") macro as is extract the two digits from the actual default year (i.e, 14) or from an optional argument (e.g. \sy[1939]).

This argument also change \the\year for later use (e.g. in \today to obtain October 9,1939)., but if you do not like that feature is quite obvious how remove it.

Finally, as I cannot imagine the benefit of using a two digit format for dates outside the XX or XXI century in real life, the argument only work with 4 digits in order to detect typing mistakes (otherwise print a warning box) with the help of the xifthen package. If you do not like this feature either, go to the simple answer.

\documentclass{article}
\usepackage{xstring,xifthen}
\newcommand{\sy}[1][]{
\StrLen{#1}[\yearlength]%
\ifthenelse{\yearlength = 4}% Only 4 character allowed 
{\year#1\relax\StrGobbleLeft{#1}{2}}%
{\ifthenelse{\yearlength = 0} % What by default 
{\StrGobbleLeft{\the\year}{2}}%
{\fbox{\bfseries\ttfamily OOPS! typed #1}}}}%


\begin{document}
Really today is \today.\par
% Correct use with default year
World War III will start after  \sy\ (... the year \the\year, obviously).\par
% Mistyped date that go to Roman times, so year is not changed.  
World War II started in the \sy[193]\ (... still stuck in \the\year).\par
% Correct use with fixed year   
World War I started in \sy[1914]\ (we mean now \the\year !).\par
Now \verb|\the\year| is still  \the\year, and \verb|\today| is \today.\par
% Mistyped date that go to Star Trek era, so year is not changed.  
Franco-Prussian war started in \sy[18870].\par 
\end{document}

MWE

|improve this answer|||||

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.