2

I have a .tex file which is named 20160201.tex, and within that file, I would like to extract the filename without .tex and convert it to the string February 01, 2016? I am able to get the string via the following code, however, I cannot find an easy way to do the conversion.

\documentclass{article}
\usepackage{currfile}

\begin{document}
This file was created on \currfilebase
\end{document}

3 Answers 3

2

You can use a macro with 8 arguments, each of which will be one of the characters in the file name, then use \ifcase to print the month name:

enter image description here

\documentclass{article}
\usepackage{currfile}

\newcommand\parsedate[1]{%
  \expandafter\parsedateaux#1\relax}
\def\parsedateaux#1#2#3#4#5#6#7#8#9\relax{%
  \ifcase#5#6 \or%Y Y Y Y M M D D
  January\or February\or March\or April\or May\or June\or July\or
  August\or September\or October\or November\or December\fi
  \space#7#8, #1#2#3#4}

\begin{document}
This file was created on \parsedate{\currfilebase}
\end{document}
2

I guess there is always Lua:

% TeX Program = lualatex

\documentclass{article}

\usepackage{currfile}
\usepackage{luacode}

\begin{luacode}
function formatdate(isodate)
  local y, m, d = string.match(isodate, "(%d%d%d%d)(%d%d)(%d%d)")
  tex.sprint(os.date("%B %d, %Y", os.time({year=y, month=m, day=d})))
end
\end{luacode}

\newcommand*{\filedate}[1][\currfilebase]{\directlua{formatdate(#1)}}

\begin{document}
This file was created on \filedate.
\end{document}

output

1

I think I found a solution using the xstring and datetime packages

\documentclass{article}
\usepackage{currfile}
\usepackage{xstring}
\usepackage{datetime}

\newcommand{\numdate}{%
\StrLeft{\currfilebase}{4}[\yearstr]%
\StrRight{\currfilebase}{2}[\daystr]%
\StrMid{\currfilebase}{5}{6}[\monthstr]%
\monthname[\monthstr]  \daystr, \yearstr}

\begin{document}
This file was created on \numdate
\end{document}
1
  • 2
    Nice solution, but don't use \year, \day and \month there: they are primitives that hold the current year, day and month, and should not be redefined. Nov 4, 2021 at 23:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .