13

I need to split a date (German format) and store its single elements in variables (to use it in TikZ-calendar).

A German formatted date looks like 23.01.2012 (Day.Month.Year), so I could use the dots as a separation marker.

Unfortunately I have no idea how to start. Has anyone suggestions or done this before?

doncherry
  • 54,637
schmendrich
  • 3,908
  • I was going to suggest the isodate package, but I don't think it can actually do what you want… – Seamus Jan 23 '12 at 16:42

3 Answers3

13

For a simple split function you can use a delimited macro. You could simply do this:

\def\mysplit#1.#2.#3.{\def\myday{#1}\def\mymonth{#2}\def\myyear{#3}}
\def\splitdate#1{\expandafter\mysplit#1.}
\documentclass{article}
\begin{document}
  \noindent We have the date: 23.01.2012 \splitdate{23.01.2012}\\
  Day: \myday\\
  Month: \mymonth\\
  Year: \myyear
\end{document}

result:

date split in separate variables

Note that right now this approach lacks any kind of error handling for wrongly formatted output.

Edit: Some additional explanation regarding the \expandafter.

The difference is the added \expandafter. What happens when using \splitdate{<argument>} then \mysplit#1 is replaced by \mysplit<argument to \splitdate>. When <argument to \splitdate> is not a date formatted like dd.mm.yyyy but a macro \mydate which is defined as \def\mydate{10.10.2010} then \mysplit\mydate does not match the definition of \mysplit (since that needs arguments delimited by .). When we use \expandafter, \mysplit is left untouched and \mydate is expanded first. This leads to \mysplit10.10.2010, then \mysplit is expanded and the arguments match the definition. When we have an additional layer, like \def\mydate{12.12.2012}\def\otherdate{\mydate} then this will still fail since \expandafter will only expand the token once. We could make this work as well, by adding a local expanded definition which we use. The complete code would then look like this:

\def\mysplit#1.#2.#3.{\gdef\myday{#1}\gdef\mymonth{#2}\gdef\myyear{#3}}
\def\splitdate#1{{\edef\x{#1}\expandafter\mysplit\x.}}
\documentclass{article}
\begin{document}
  \def\mydate{23.01.2012}
  \def\deepdate{\mydate}
  \noindent We have the date: 23.01.2012 \splitdate{\deepdate}\\
  Day: \myday\\
  Month: \mymonth\\
  Year: \myyear
\end{document}

Note that we had to change the definition of \myday etc. to global.

David Carlisle
  • 757,742
Roelof Spijker
  • 17,663
  • 5
  • 55
  • 63
5

You could also use \StrBefore, \StrBetween, and \StrBehind from the the xstring package:

enter image description here

\documentclass{article}
\usepackage{xstring}

\newcommand*{\ExtractDay}[1]{\StrBefore[1]{#1}{.}}%
\newcommand*{\ExtractMonth}[1]{\StrBetween[1,2]{#1}{.}{.}}%
\newcommand*{\ExtractYear}[1]{\StrBehind[2]{#1}{.}}%

\newcommand*{\MySplit}[1]{%
    \def\MyDay{\ExtractDay{#1}}%
    \def\MyMonth{\ExtractMonth{#1}}%
    \def\MyYear{\ExtractYear{#1}}%
}%

\begin{document}
  \noindent We have the date: 23.01.2012\\
  Day:   \ExtractDay{23.01.2012}\\
  Month: \ExtractMonth{23.01.2012}\\
  Year:  \ExtractYear{23.01.2012}

  \bigskip
  \noindent If you prefer to have separate macros define for each component

  \noindent We have the date: 23.01.2012 \MySplit{23.01.2012}\\
  Day:   \MyDay\\
  Month: \MyMonth\\
  Year:  \MyYear
\end{document}
Moriambar
  • 11,466
Peter Grill
  • 223,288
1

just use \year, \month and \day to get the different parts of the date. they work as integer registers. so you can use

\the\year.\the\month.\the\day

to get what you want.

Leo Liu
  • 77,365