datetime2:格式化时间

datetime2:格式化时间

09:00 AM我怎样才能获得而不是 的输出9am

\documentclass{article}
\usepackage[en-GB]{datetime2}

\begin{document}
\DTMtime{09:00:00}

The desired output is ``09:00 AM"
\end{document}

在此处输入图片描述

答案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}

相关内容