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?

  • 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
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.

  • 4
    I would at least do \def\splitdate#1{\expandafter\mysplit#1.} to have the argument expanded once. This way people can do something like \splitdate{\mydate}. – Martin Scharrer Jan 23 '12 at 14:19
  • @MartinScharrer: Agreed, I added it. – Roelof Spijker Jan 23 '12 at 14:45
  • Would you explain the difference please? – schmendrich Jan 23 '12 at 15:06
  • 1
    @schmendrich: added some explanation. – Roelof Spijker Jan 23 '12 at 15:45
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}
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.

  • 3
    This refers only to today's date. A calendar usually has more than this. – egreg Jan 23 '12 at 14:10
  • Isn't that only for the date today? – schmendrich Jan 23 '12 at 14:12

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.