# Date translated to Chinese

The English dates are strings like 1 January 2016. To translated them in Chinese, I use the following way with the help of Date Calculations.

\usepackage{advdate,xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\StrMonthToNum}{ m }
{
\str_case:nnF { #1 } {
{ January   } {  1 }
{ February  } {  2 }
{ March     } {  3 }
{ April     } {  4 }
{ May       } {  5 }
{ June      } {  6 }
{ July      } {  7 }
{ August    } {  8 }
{ September } {  9 }
{ October   } { 10 }
{ November  } { 11 }
{ December  } { 12 }
} { 12 } % Wrong month, defaults to December/12
}
\ExplSyntaxOff

\begin{document}
\begin{CJK}{UTF8}{gbsn}
\def\cndate #1 #2 #3 {%
#3年% Set year
\StrMonthToNum{#2}月% Set month
#1日% Set day
}

\cndate 1 January 2016

\end{CJK}
\end{document}


The output is showed:

However, the dates are stored in the commands like \newcommand{\ENGLISHDATE}{1 January 2016}. Are there any way to get it if it should be executed like this: \cndate{\ENGLISHDATE{}}?

• At the moment I don't see why you need advdate – user31729 Jan 12 '16 at 9:07

I don't have CJK at hand -- I replaced the environment and the Chinese symbols by other symbols.

The \cndate macro explicitly expects 3 arguments, delimited by white space. This is not given by a simple macro like \mydate or \ENGLISHDATE.

I suggest to rename \cndate to \cndateinternal and define \cndate to be

\def\cndate#1{%
\expandafter\cndateinternal#1%
}


This will expand the argument #1 first and as such 'prepares' the input for \cndateinternal.

\documentclass{article}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\StrMonthToNum}{+m }
{
\str_case:nnF { #1 } {
{ January   } {  1 }
{ February  } {  2 }
{ March     } {  3 }
{ April     } {  4 }
{ May       } {  5 }
{ June      } {  6 }
{ July      } {  7 }
{ August    } {  8 }
{ September } {  9 }
{ October   } { 10 }
{ November  } { 11 }
{ December  } { 12 }
} { 12 } % Wrong month, defaults to December/12
}
\ExplSyntaxOff

\newcommand{\mydate}{29 February 2016}

\begin{document}

\def\cndate#1{%
\expandafter\cndateinternal#1%
}

\def\cndateinternal#1 #2 #3 {%
#3$\dagger$% Set year
\StrMonthToNum{#2}$\sum$% Set month
#1$\pi$% Set day
}

\cndateinternal 1 January 2016 %
\cndate 5 March 2017 %
\cndate{\mydate}  % Braces needed

\end{document}


Update

Here's a version with a wrapper again that takes care about eventual outer parentheses etc.

\documentclass{article}
\usepackage{xparse}
\usepackage{l3regex}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\StrMonthToNum}{+m }
{
\str_case_x:nnF { #1 } {
{ January   } {  1 }
{ February  } {  2 }
{ March     } {  3 }
{ April     } {  4 }
{ May       } {  5 }
{ June      } {  6 }
{ July      } {  7 }
{ August    } {  8 }
{ September } {  9 }
{ October   } { 10 }
{ November  } { 11 }
{ December  } { 12 }
} { 12 } % Wrong month, defaults to December/12
}

\NewDocumentCommand{\CnDate}{+m}{%
\tl_set:Nx \l_tmpa_tl {#1} % Expand the date string to a token list
\tl_trim_spaces:N \l_tmpa_tl % Remove spaces tail and head
\regex_replace_all:nnN {\s} {;} \l_tmpa_tl  % Replace space '\s with ;
\seq_set_split:NnV \l_tmpa_seq {;} {\l_tmpa_tl} % Split the sequence
\DisplayCnDate{\seq_item:Nn \l_tmpa_seq {1}}{\seq_item:Nn \l_tmpa_seq {2}}{\seq_item:Nn \l_tmpa_seq {3}} % Display the date
}
\ExplSyntaxOff

\NewDocumentCommand{\DisplayCnDate}{mmm}{%
#3$\dagger$% Set year
\StrMonthToNum{#2}$\sum$% Set month
#1$\pi$% Set day
}

\newcommand{\mydate}{29 February 2016}

\begin{document}

\CnDate{\mydate} %works

\CnDate \mydate %works

\CnDate 15 January 2016 % Does not work
\DisplayCnDate 15 January 2016 % Does not work too

\CnDate{ 15 January 2016                  } % works

(\CnDate{ 15 January 2016                  }) % works

\end{document}


• I also tried this before and I realized if I have other words following like (\cndate{\mydate}), it will output (2016)†2 29π. Do you know a way that the date will not be affected by the symbols or other words around? – Joe Jan 12 '16 at 9:51
• @Joe: I think, this is not really easy (at least this way). Since your using expl3` features anyway, I think it's better to exploit that features rather – user31729 Jan 12 '16 at 10:41