# Get Weekdayname from custom date

I'm new to LaTeX and need help.

I want to convert a input of this date format: 20161027 to this output:
"Thursday, 27.10.2016"

I tried around with the packages xstring and datenumber.

The following code runs into an error.

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[ngerman]{datenumber}
\usepackage{xstring}
\usepackage[left=2.00cm, right=2.50cm, top=6.50cm, bottom=3.00cm]{geometry}

\newcommand{\shortdat}{\datedayname {,} \thedateday.\thedatemonth.\thedateyear}
\newcommand{\stringtosub}{\nul}
\newcommand{\substring}[2][1]{
\ifx\\#2\\
{\stringtosub}
\else
\StrMid{\stringtosub}{#1}{#2}
\fi
}
\newcommand{\operation}[1]{
\renewcommand{\stringtosub}{#1}
\substring[1]{4}.\substring[5]{6}.\substring[7]{8}
\setdate{\substring[1]{4}}{\substring[5]{6}}{\substring[7]{8}}
\shortdat
}

\begin{document}
first \operation{20161025}
\\
next \operation{20161027}
\end{document}


How can I solve it?

• Welcome to TeX.SX. I haven't used the package myself so far, but from my mind I'd say that the datetime2 package from Nicola Talbot could do what you want. – Stefan Pinnow Oct 27 '16 at 18:46

Here is an option using datetime2:

\documentclass{article}

\usepackage[calc,english]{datetime2}
\makeatletter
\newcommand{\operation}[1]{\@operation #1\relax}
\def\@operation #1#2#3#4#5#6#7#8\relax{{%
\DTMsavedate{saveddate}{#1#2#3#4-#5#6-#7#8}%
\DTMsetdatestyle{mydate}% Set new date style
\DTMusedate{saveddate}% Set date
}}
\DTMnewdatestyle{mydate}{%
% #1 = year
% #2 = month
% #3 = day
% #4 = day of week
\renewcommand{\DTMdisplaydate}[4]{%
\DTMweekdayname{##4}, \DTMtwodigits{##3}.\DTMtwodigits{##2}.\number##1}%
}

\begin{document}

first \operation{20161025}

next \operation{20161027}

\end{document}


The implementation assumes that your input for \operation will always be of the form YYYYMMDD (in other words, two-digit representations for the month and day).

It's much simpler: just absorb the digits as macro arguments.

\documentclass[11pt,a4paper]{article}
\usepackage[ngerman]{datenumber}

\newcommand{\operation}[1]{\splitdate#1}
\newcommand{\splitdate}[8]{%
\begingroup % to keep \setdate local
\setdate{#1#2#3#4}{#5#6}{#7#8}%
\datedayname,~#7#8.#5#6.#1#2#3#4%
% if you want to omit leading zeros use the line below
%\datedayname,~\number#7#8.\number#5#6.#1#2#3#4%
\endgroup
}

\begin{document}

first \operation{20161025}

next \operation{20161027}

\end{document}


If you also want to check the length of the argument to be eight characters, you can do as follows:

\documentclass[11pt,a4paper]{article}
\usepackage{xparse}
\usepackage[ngerman]{datenumber}

\ExplSyntaxOn
\NewDocumentCommand{\operation}{m}
{
\int_compare:nTF { \tl_count:n { #1 } = 8 }
{
}
{
\errmessage{Invalid~date}
ERR-#1-ERR
}
}
{
\group_begin:
\setdate{#1#2#3#4}{#5#6}{#7#8}
\datedayname,~#7#8.#5#6.#1#2#3#4
% if you want to omit leading zeros use the line below
%\datedayname,~\number#7#8.\number#5#6.#1#2#3#4
\group_end:
}
\ExplSyntaxOff

\begin{document}

first \operation{20161025}

next \operation{20161027}

error \operation{1233}

error \operation{123456789}

\end{document}


Invalid values of month and day will be signalled by datenumber.

• Doesn't Werner's code also absorb them as macro arguments? Do you mean that yours is simpler because they are absorbed directly? – cfr Oct 27 '16 at 23:02
• @cfr: The absorption is the same; egreg just wanted to post something different using expl3... – Werner Oct 27 '16 at 23:10
• @Werner Indeed ;). – cfr Oct 27 '16 at 23:13
• @cfr I used datenumber – egreg Oct 27 '16 at 23:20