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

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

  \noindent We have the date: 23.01.2012 \splitdate{23.01.2012}\\
  Day: \myday\\
  Month: \mymonth\\
  Year: \myyear


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:

  \noindent We have the date: 23.01.2012 \splitdate{\deepdate}\\
  Day: \myday\\
  Month: \mymonth\\
  Year: \myyear

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

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

enter image description here




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

  \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

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


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.