0

How can I get an output of 09:00 AM instead of 9am?

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

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

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

enter image description here

1

You can define your own time style.

\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}

enter image description here

0

Another answer was provided in @Werner's comment

\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}

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.