我想让以下文档打印输出07:00 PM
。
\documentclass{article}
\usepackage[calc,english]{datetime2}
\DTMnewtimestyle{ampm}{%
% \DTMdisplaytime{<hh>}{<mm>}{<ss>}
\renewcommand{\DTMdisplaytime}[3]{%
\def\THEHOUR{##1}\def\THEAMPM{AM}% Assume AM
\ifnum##1>12
\edef\THEHOUR{\number\numexpr##1-12}% Correct for...
\edef\THEAMPM{PM}% ...PM
\fi
\DTMtwodigits{\THEHOUR}% <hh>
:%
\DTMtwodigits{##2}% <mm>
\space
\THEAMPM% AM/PM
}%
}
\begin{document}
\DTMsettimestyle{ampm}
\DTMtime{19}
\end{document}
答案1
显然\DTMtime
需要在表格中输入hh:mm:ss
(但每个部分只能使用一位数字)。
您可以使用以下方式补充缺失的部分xparse
:
\documentclass{article}
\usepackage[calc,english]{datetime2}
\usepackage{xparse}
\NewDocumentCommand{\xDTMtime}{>{\SplitArgument{2}{:}}m}{\xxDTMtime#1}
\NewDocumentCommand{\xxDTMtime}{mmm}{%
\IfNoValueTF{#2}{%
\DTMtime{#1:00:00}%
}{%
\IfNoValueTF{#3}{%
\DTMtime{#1:#2:00}%
}{%
\DTMtime{#1:#2:#3}%
}%
}%
}
\DTMnewtimestyle{ampm}{%
% \DTMdisplaytime{<hh>}{<mm>}{<ss>}
\renewcommand{\DTMdisplaytime}[3]{%
\def\THEHOUR{##1}\def\THEAMPM{AM}% Assume AM
\ifnum##1>12
\edef\THEHOUR{\number\numexpr##1-12}% Correct for...
\edef\THEAMPM{PM}% ...PM
\fi
\DTMtwodigits{\THEHOUR}% <hh>
:%
\DTMtwodigits{##2}% <mm>
~%
\THEAMPM% AM/PM
}%
}
\begin{document}
\DTMsettimestyle{ampm}
\xDTMtime{19}
\xDTMtime{19:15}
\xDTMtime{19:1:2}
\end{document}