2

A date is predefined in the format 12 June 2015 and I need package datetime2 to modify the date and translate it in other languages. However, it does not work in the following way:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish,english]{babel}
\usepackage{xparse}
\usepackage{datetime2}
\DTMsetup{useregional}
\DTMsetstyle{ddmmyyyy}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\StrMonthToNum}{ m }
{
  \str_case:nnF { #1 } {
    { January   } { 01 }
    { February  } { 02 }
    { March     } { 03 }
    { April     } { 04 }
    { May       } { 05 }
    { June      } { 06 }
    { July      } { 07 }
    { August    } { 08 }
    { September } { 09 }
    { October   } { 10 }
    { November  } { 11 }
    { December  } { 12 }
  } { 12 } % Wrong month, defaults to December/12
}
\ExplSyntaxOff

% Change from `DD MMtext YYYY' to `YYYY-MM-DD'
\def\dtdateformat#1{\expandafter\dtghiredate#1\relax}
\def\dtghiredate#1 #2 #3\relax{#3-\StrMonthToNum{#2}-#1}

\newcommand{\originaldate}{12 June 2016}

\begin{document}
\selectlanguage{english}
\DTMsavedate{changeddate}{\dtdateformat{\originaldate}}
\DTMusedate{changeddate}

\selectlanguage{spanish}
\DTMusedate{changeddate}


\end{document}

How could it work?

2

Yes, it's an expansion problem: \DTMsavedate doesn't do it.

You can solve it by

\begingroup\edef\x{\endgroup
  \noexpand\DTMsavedate{changeddate}{\dtdateformat{\originaldate}}%
}\x

or, more neatly, by defining an abstraction:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish,english]{babel}
\usepackage{xparse}
\usepackage{datetime2}
\DTMsetup{useregional}
\DTMsetstyle{ddmmyyyy}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\StrMonthToNum}{ m }
{
  \str_case:nnF { #1 } {
    { January   } { 01 }
    { February  } { 02 }
    { March     } { 03 }
    { April     } { 04 }
    { May       } { 05 }
    { June      } { 06 }
    { July      } { 07 }
    { August    } { 08 }
    { September } { 09 }
    { October   } { 10 }
    { November  } { 11 }
    { December  } { 12 }
  } { 12 } % Wrong month, defaults to December/12
}
\ExplSyntaxOff

% Change from `DD MMtext YYYY' to `YYYY-MM-DD'
\def\dtdateformat#1{\expandafter\dtghiredate#1\relax}
\def\dtghiredate#1 #2 #3\relax{#3-\StrMonthToNum{#2}-#1}
\def\eDTMsavedate#1#2{%
  \begingroup\edef\x{\endgroup
    \noexpand\DTMsavedate{#1}{#2}%
  }\x
}

\newcommand{\originaldate}{12 June 2016}

\begin{document}

\eDTMsavedate{changeddate}{\dtdateformat{\originaldate}}%
\DTMusedate{changeddate}

\selectlanguage{spanish}
\DTMusedate{changeddate}


\end{document}

enter image description here

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.