Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Possible Duplicate:
How do I split a date?

Does anyone know how to extract year from date in parameter of own macro?

Namely,

\themacro#1{...}

where #1 contain either date already in format yyyy or in format dd.mm.yyyy, which \themacro would convert to plain yyyy...

I hope I made myself clear, well, simply speaking, I just need to extract the year from date (if extraction is necessary).

share|improve this question
    
Have a look to the first answer of the question how-do-i-split-a-date. –  Kurt Aug 23 '12 at 17:22
add comment

marked as duplicate by Werner, egreg, Tom Bombadil, Marco Daniel, cgnieder Aug 24 '12 at 20:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 5 down vote accepted

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. As @Kurt pointed out, 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.

share|improve this answer
    
Wow, I just went back to my answer and realized it was totally broken. I'm not really sure why you accepted it in that state, but anyway, now it is fixed and properly working. –  Samuel Albert Aug 23 '12 at 22:21
add comment

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:

enter image description here

share|improve this answer
    
Thank you for your answer. This truly is simple, clear and elegant solution. Though, I was originally looking for vanilla plainTeX solution. Still, I do deeply appreciate your help and click to button up :-) –  mathemage Aug 23 '12 at 20:37
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.