答案1
您可以定义自己的时间风格。
\documentclass{article}
\usepackage{datetime2}
\usepackage{calculator}
\DTMnewtimestyle{custom}{
\renewcommand{\DTMdisplaytime}[3]{
\ifnum##1<12
\DTMtexorpdfstring{\DTMtwodigits{##1}:\DTMtwodigits{##2} AM}
\fi
\ifnum##1>12
\ADD{##1}{-12}{\hour}
\DTMtexorpdfstring{\DTMtwodigits{\hour}:\DTMtwodigits{##2} PM}
\fi
\ifnum##1=12
\DTMtexorpdfstring{12:\DTMtwodigits{##2} PM}
\fi
}
}
\DTMsettimestyle{custom}
\begin{document}
\DTMtime{09:00:00} and\DTMtime{12:30:00} and\DTMtime{18:00:00}.
Caution: Don't use \verb`\DTMtime{}` more than 23:59:59!
\end{document}
答案2
另一个答案是@Werner 的评论
\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
\THEHOUR% <hh>
:%
\DTMtwodigits{##2}% <mm>
\space
\THEAMPM% AM/PM
}%
}
\begin{document}
\DTMsettimestyle{ampm}
\DTMcurrenttime
\end{document}