3

Is it possible to have datetime return the current time and date in the following format?

Saturday 25. February 2017, 01:24:01 PM CET.

Werner
  • 603,163
E. l4d3
  • 697
  • Related: https://tex.stackexchange.com/questions/390693/datetime-ranges-using-datetime2/390738#390738 –  Oct 17 '17 at 22:00

2 Answers2

5

Use datetime2 and create a new style:

enter image description here

\documentclass{article}

\usepackage[calc,english]{datetime2}

% Date/time style:
% <dow> <dayofmonth>. <monthname> <yyyy>, <hh>:<mm>:<ss> <ampm> CET
% Example: Saturday 25. February 2017, 01:24:01 PM CET
\DTMnewdatestyle{mydatestyle}{%
  % \DTMdisplaystyle{<YYYY>}{<MM>}{<DD>}{<dow>}
  \renewcommand{\DTMdisplaydate}[4]{%
    \DTMsettimestyle{mytimestyle}% Set the time style
    \DTMweekdayname{##4}% Day of week
    \space
    ##3% Day of month
    . % Period
    \DTMmonthname{##2}% Month of year
    \space
    ##1% Year
    , % Comma
    \DTMcurrenttime
  }%
  \renewcommand{\DTMDisplaydate}{\DTMdisplaydate}%
}

\DTMnewtimestyle{mytimestyle}{%
  % \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>
    :%
    \DTMtwodigits{##2}% <ss>
    \space
    \THEAMPM% AM/PM
    \space
    CET% CET
  }%
}

\begin{document}

\DTMsetdatestyle{mydatestyle}

Today is \today.

\end{document}
Werner
  • 603,163
3

This is very similar to the en-GB style provided with datetime2-english:

\documentclass{article}

\usepackage[en-GB]{datetime2}

\newcommand*{\DTMenGBzonemaps}{%
  \DTMdefzonemap{01}{00}{CET}%
}    
\DTMenGBzonemaps

\DTMlangsetup[en-GB]{
 mapzone,
 ord=omit,
 daymonthsep={.\space},
 datetimesep={,\space}}

\begin{document}
\DTMnow
\end{document}

This isn't an exact match but it's quite close:

18. October 2017, 11:56am CET

The am/pm part can easily be converted to upper case with:

\renewcommand{\DTMenglishampmfmt}[1]{\MakeUppercase{#1}}

The englishampm time style doesn't support seconds or two-digit hours. I really don't recommend two-digit hours with an am/pm style as a casual glance might interpret a leading zero as indicating twenty-four hour format without noticing the am/pm part. If you really need it, you can redefine the englishampm style as follows:

\DTMrenewtimestyle
 {englishampm}% label
 {%
    \renewcommand*\DTMdisplaytime[3]{%
      \ifnum##2=0
        \ifnum##1=12
          \DTMtwodigits{##1}\DTMenglishtimesep
          \DTMtwodigits{##2}\DTMenglishtimesep
          \DTMtwodigits{##3}%
          \space
          \ifnum##3=0
            \DTMtexorpdfstring
              {\DTMenglishampmfmt{\DTMenglishnoon}}%
              {\DTMenglishnoon}%
          \else
            \DTMtexorpdfstring
              {\DTMenglishampmfmt{\DTMenglishpm}}%
              {\DTMenglishpm}%
          \fi
        \else
          \ifnum##1=0
            \DTMtwodigits{##1}\DTMenglishtimesep
            \DTMtwodigits{##2}\DTMenglishtimesep
            \DTMtwodigits{##3}%
            \space
            \ifnum##3=0
              \DTMtexorpdfstring
              {\DTMenglishampmfmt{\DTMenglishmidnight}}%
              {\DTMenglishmidnight}%
            \else
              \DTMtexorpdfstring
              {\DTMenglishampmfmt{\DTMenglisham}}%
              {\DTMenglisham}%
            \fi
          \else
            \ifnum##1=24
              00\DTMenglishtimesep
              \ifnum##3=0
                \DTMtwodigits{##2}\DTMenglishtimesep
                \DTMtwodigits{##3}%
                \space
                \DTMtexorpdfstring
                {\DTMenglishampmfmt{\DTMenglishmidnight}}%
                {\DTMenglishmidnight}%
              \else
                \DTMtexorpdfstring
                {\DTMenglishampmfmt{\DTMenglisham}}%
                {\DTMenglisham}%
              \fi
            \else
              \ifnum##1<12
                \DTMtwodigits{##1}\DTMenglishtimesep
                \DTMtwodigits{##2}\DTMenglishtimesep
                \DTMtwodigits{##3}%
                \space
                \DTMtexorpdfstring
                {\DTMenglishampmfmt{\DTMenglisham}}%
                {\DTMenglisham}%
              \else
                \DTMtwodigits{\numexpr##1-12}\DTMenglishtimesep
                \DTMtwodigits{##2}\DTMenglishtimesep
                \DTMtwodigits{##3}%
                \space
                \DTMtexorpdfstring
                {\DTMenglishampmfmt{\DTMenglishpm}}%
                {\DTMenglishpm}%
              \fi
            \fi
          \fi
        \fi
      \else
        \ifnum##1<13
          \ifnum##1=0
            12%
          \else
            \DTMtwodigits{##1}%
          \fi
          \DTMenglishtimesep\DTMtwodigits{##2}%
          \DTMenglishtimesep\DTMtwodigits{##3}%
          \space
          \ifnum##1=12
            \DTMtexorpdfstring
            {\DTMenglishampmfmt{\DTMenglishpm}}%
            {\DTMenglishpm}%
          \else
            \DTMtexorpdfstring
            {\DTMenglishampmfmt{\DTMenglisham}}%
            {\DTMenglisham}%
          \fi
        \else
          \DTMtwodigits{\number\numexpr##1-12}%
          \DTMenglishtimesep\DTMtwodigits{##2}%
          \DTMenglishtimesep\DTMtwodigits{##3}%
          \space
          \ifnum##1=24
            \DTMtexorpdfstring
            {\DTMenglishampmfmt{\DTMenglisham}}%
            {\DTMenglisham}%
          \else
            \DTMtexorpdfstring
            {\DTMenglishampmfmt{\DTMenglishpm}}%
            {\DTMenglishpm}%
          \fi
        \fi
      \fi
    }%
 }%

The style then needs resetting:

\DTMsettimestyle{englishampm}

This style definition has the advantage over the style in Werner's answer in that it's fully expandable, so if you add hyperref and use:

\begin{document}
\tableofcontents
\section{\DTMnow}
\end{document}

then the time will appear correctly in the PDF bookmark.

Nicola Talbot
  • 41,153