Extract year from parameter containing date
Try \StrRight
in the xstring
package:
\documentclass{article}
\usepackage{xstring}
\def\getYear#1{\StrRight{#1}{4}}
\begin{document}
\noindent\getYear{12.12.2012}\\
\getYear{2012}
\end{document}
with result:
user1189687 already proposed a solution that I think is better, but since I already begun to write this, here is if you want a plain tex solution :
The point here is to recognize that you are in the first case or in the other. It is trivial to do if you know the format of your input. But here, apparently you don't.
So what I would do would be :
\makeatletter
\def\@themacro #1.#2\end@themacro{%
\def\@tempyr{#1}%
\def\@temprem{#2}%
}%
%
\def\themacro #1 {%
\let\day\relax%
\let\month\relax%
\let\year\relax%
\let\@tempyr\relax%
\let\@temprem\relax%
\@themacro#1.\relax\end@themacro% get day or year
\if\relax\@temprem\relax%
\if\relax\@tempyr\relax%
\GenericError{}{Wrong date format}{}{}% Manage the error
\else%
\edef\year{\expandafter\noexpand\@tempyr}%
\fi%
\else%
\edef\day{\expandafter\noexpand\@tempyr}%
\expandafter\@themacro\@temprem\end@themacro% get month
\if\relax\@temprem\relax%
\GenericError{}{Wrong date format}{}{}% Manage the Error
\else%
\edef\month{\expandafter\noexpand\@tempyr}%
\expandafter\@themacro\@temprem\end@themacro% get year
\if\relax\@temprem\relax%
\edef\year{\expandafter\noexpand\@tempyr}%
\else%
\GenericError{}{Wrong date format}{}{}% Manage the Error
\fi%
\fi%
\fi%
}%
\makeatother%
The advantage of this solution is that you detect incorect syntax and can have a fallback or throw an error, as you wish.
Oh, I almost forgot ... You of course get the year as the replacement text of \year
.
In case they exist, you also can get \day
and \month
.