4

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.

1

4 Answers 4

6

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}
2
  • If you don't mind, I would be grateful to know what command results in typesetting the time as, for example, 9:00 AM?
    – Diaa
    Commented Jan 24, 2019 at 22:51
  • @Diaa: Try this.
    – Werner
    Commented Jan 25, 2019 at 0:01
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.

1

I think Werner's brilliant answer is not quite correct. A time of day from 00:00:00 to 00:59:59 as 00:09:23, all ISO is written as 12:09:23 am and a time of day from 12:00:00 to 12:59:59 as 12:09:23 all ISO is written as 12:09:23 pm. The correct version of Werner's code should therefore read as follows

\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%
    \ifnum##1=12
      \edef\THEHOUR{12}% Correct for...
      \edef\THEAMPM{PM}%                    ...PM
    \fi%
     \ifnum##1=0
      \edef\THEHOUR{12}% Correct for...
      \edef\THEAMPM{AM}%                    ...AM
    \fi%
    \THEHOUR% <hh>
    :%
    \DTMtwodigits{##2}% <mm>
    :%
    \DTMtwodigits{##2}% <ss>
    \space
    \THEAMPM% AM/PM
    \space
    CET% CET
  }%
}
1

The following code can be used in OpTeX:

\setweekday \setminutes \setseconds % Initializes \weekday, \hours, \minutes, \seconds.
    % Note that TeX primitives \day, \monts, \year are initialized by default.
\def\printweekday{\ifcase\weekday Sun\or Mon\or Tues\or Wednes\or Thurs\or Fri\or Satur\fi day}
\def\printdate{%
   \printweekday~\the\day. \cs{_mt:m\the\month:en} \the\year, 
   \printhour:\Othe\minutes:\Othe\seconds~\AMorPM~CET
}
\def\printhour{% We distinguish between PM and AM
   \ifnum\hours>12 \tmpnum=\numexpr\hours-12 \def\AMorPM{PM}%
   \else \tmpnum=\hours \def\AMorPM{AM}\fi
   \Othe\tmpnum
}

Today is \printdate.

\bye

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .