1

I'm learning a little TeX. The following code works fine. But I don't know how to add a leading zero for hour and for minute, if each one of them is less than 10.

Thank you for your help.

This code works fine. The output:

The military time is: 1221 The standard time is: 20:21

\documentclass[]{article}

\setlength\parindent{0in}

\newcount\Hour
\newcount\Minute
\Hour=\time
\divide\Hour by 60
\Minute =\Hour
\multiply\Minute by 60
\advance\Minute by -\time
\Minute = -\Minute


\begin{document}

The military time is: \the\time\\
The standard time is: \the\Hour{:}\the\Minute

\end{document}
5
  • I assume by "military time", you mean GMT (Greenwich Mean Time)? And "standard time" is determined by the time zone you're in, right?
    – Mico
    Mar 27 at 20:50
  • 7
    @Mico I would interpret "military time" differently; namely 24-hour clock. So I think the OP needs to clarify meanings. Mar 27 at 20:51
  • 2
    \time gives minutes since midnight (local time), so your assertion of military time = \the\time is not correct under any definition of "military time" that I am familiar with. Mar 27 at 20:55
  • When writing the code, my time zone was 20:21pm. My code converted it to 2021. An other example. Military time: 0345. standard time is 5:45. This is the best I could offer for clarification. Oh, by the way just try this command alone: \the\time.
    – Rafiq
    Mar 27 at 20:59
  • Specifying time is a complex problem outside of Latex. It may not be a useful subject to start learning Latex, which you can read from many comments and answeres here. Some very good links were already given in this answer here: tex.stackexchange.com/a/714499/245790 .
    – MS-SPO
    Apr 1 at 10:06

5 Answers 5

9

While the definition of "military" time is unclear, the issue of single digit hours and minutes is resolved via \ifnum...<10 test:

\documentclass[]{article}

\setlength\parindent{0in}

\newcount\Hour
\newcount\Minute
\Hour=\time
\divide\Hour by 60
\Minute =\Hour
\multiply\Minute by 60
\advance\Minute by -\time
\Minute = -\Minute
\def\tdhour{\ifnum\Hour<10 0\the\Hour\else\the\Hour\fi}
\def\tdmin{\ifnum\Minute<10 0\the\Minute\else\the\Minute\fi}

\begin{document}

The military time is: \the\time\\
The standard time is: \tdhour:\tdmin
\end{document}

enter image description here

The \td... macros may be more simply written as

\def\tdhour{\ifnum\Hour<10 0\fi\the\Hour}
\def\tdmin{\ifnum\Minute<10 0\fi\the\Minute}
1
  • 2
    Steven B. Segletes- Thank you so much for your help.
    – Rafiq
    Mar 27 at 21:07
6

I'm not sure what's “military time”. When you start a TeX run, the internal counter \time is initialized with the number of minutes past midnight.

For instance, if I start a run of TeX, I see 1340, which corresponds to 22:20 (local time).

You don't need counters to get the time in hh:mm format, because the LaTeX kernel already has counters that hold the current hour and current minute, so you just have to wrap them in a command that adds a leading zero if the number is less than 10.

\documentclass{article}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\printtime}{}
 {
  \rafiq_twodigits:n { \c_sys_hour_int }
  :
  \rafiq_twodigits:n { \c_sys_minute_int }
 }
\cs_new:Nn \rafiq_twodigits:n
 {
  \int_compare:nNnT { #1 } < { 10 } { 0 } % perhaps add a leading 0
  \int_to_arabic:n { #1 } % print the number
 }

\ExplSyntaxOff

\begin{document}

Minutes past midnight: \the\time

Local time: \printtime

\end{document}

enter image description here

1
  • 1
    Thanks a lot for the information. I'm still totally new to Tex, LaTeX, let alone expl.
    – Rafiq
    Mar 27 at 22:51
4

Your question is: how to print minutes in two digit form. I am using the \Othe macro which can be defined by:

\def\Othe#1{\ifnum#1<10 0\fi\the#1}

and used:

Time is \the\hours:\Othe\minutes.

If you are using OpTeX, then this calculation (based on \directlua) is ready to use, because it is OpTeX trick 0055 with the auto-loaded macro \sethours. It sets the \hours and \minutes registers to appropriate values from the current \time register. And the \Othe macro is auto-loaded too. So, you can write only this:

\sethours

Minutes past midnight:: \the\time.

Local time: \the\hours:\Othe\minutes.

\bye
1

As the term "military time" is not clarified in the question while military practices are the same neither all over the world nor across all times, the author of the initial release of this answer decided to ignore the military aspect and to take the issue for a question about having at least k digits (via adding leading zeros if need be) when delivering a natural number represented by Arabic numerals in the positional number system with base ten.


In your example you use the ⟨integer parameter⟩ \time.

The TeXbook says in chapter 24, somewhere after exercise 24:

\time (current time of day in minutes since midnight)
[...]
TeX computes the date and time when it begins a job, if the operating system provides such information; but afterwards the clock does not keep ticking: The user can change \time just like any ordinary parameter.

However the TeXbook does not say what "midnight" is.
The phrase "if the operating system provides such information" indicates that this is platform-dependent and may vary from TeX distribution to TeX distribution and from computer platform to computer platform.

For example the real time clock with many computers

  • under Linux is set to Coordinated Universal Time (UTC) and for displaying local time calculations are done where the time-zone settings of the computer play a rôle.
  • under Windows is set to local time.

So depending on platform and TeX distribution in use you need to verify what TeX takes for midnight/what \time delivers.

You might be interested in


If you wish to calculare hours and minutes from the value of the ⟨integer parameter⟩ \time, you don't need to allocate registers \Hour and \Minute for this.
Instead you can use ε-TeX's \numexpr for doing expandable calculation.
However, unlike \divide, which truncates the result of division to an integer by just cutting off decimal places, ε-TeX's \numexpr's /-operation rounds the result to an integer.
So if you want the quotient of Euclidean division/of division with non-negative remainder of two integers rather than the quotient of division with rounding, with ε-TeX's \numexpr you need to check whether rounding up occurred. In case rounding up occurred, the result of multiplying the quotient of the division with rounding with the divisor is larger than the the dividend. In case rounding up occurred and the divisor is positive, you need to subtract 1 from the quotient of the division with rounding. In case rounding up occurred and the divisor is negative, you need to add 1 to the quotient of the division with rounding.

\documentclass[]{article}

\makeatletter
%=======================================================================
% Add leading zeros to TeX <number> if need be to get at least K digits:
%-----------------------------------------------------------------------
% \AtLeastKdigits{<minimum amount of digits=K>}{<number>}
%.......................................................................
\@ifdefinable\stopromannumeral{\chardef\stopromannumeral=`\^^00 }%
\newcommand*\AtLeastKdigits[2]{%
  \romannumeral
  \expandafter\Zeroloop
  \expandafter{\the\numexpr(#1)\expandafter\relax
                               \expandafter}%
                               \expandafter\relax
                               \the\numexpr
                               \ifnum\the\numexpr(#2)\relax<0 %
                                 \expandafter\@firstoftwo
                               \else
                                 \expandafter\@secondoftwo
                               \fi   
                               {(#2)*(-1)\relax\relax{}{-}}%
                               {(#2)\relax\relax{}{}}%
}%
\newcommand*\Zeroloop[1]{%
  \ifnum#1>0 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {\expandafter\Zeroloop\expandafter{\the\numexpr((#1)-1)\relax}0}%
  {\AtLeastKdigitsCheck}%
}%
\@ifdefinable\AtLeastKdigitsCheck{%
  \long\def\AtLeastKdigitsCheck#1\relax#2\relax#3#4{%
    \ifcat$\detokenize{#1}$%
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
    {\stopromannumeral#4#3#2}%
    {%
      \ifcat$\detokenize{#2}$%
        \expandafter\@firstoftwo
      \else
        \expandafter\@secondoftwo
      \fi
      {\stopromannumeral#4#1#3}%
      {\AtLeastKdigitsSplit#1\relax#2\relax{#3}{#4}}%
    }%
  }%
}%
\@ifdefinable\AtLeastKdigitsSplit{%
  \long\def\AtLeastKdigitsSplit#1#2\relax#3#4\relax#5#6{%
    \AtLeastKdigitsCheck#2\relax#4\relax{#5#3}{#6}%
  }%
}%
%=======================================================================
% Expandable Euclidean division:
%
%   (quotient)*(divisor)+(remainder)=(dividend);
%   <->
%   ((dividend)-(remainder))/(divisor)=(quotient);
%
%  Both quotient and dividend are elements of the set of integers.
%  Divisor is an element of the set of integers without the element 0.
%  Remainder is an element of the set of those non-negative integers
%  that are smaller than the absolute value of the divisor.
%-----------------------------------------------------------------------
% \EuclideanDivisionQuotient{<dividend>}{<divisor>}
% \EuclideanDivisionRemainder{<dividend>}{<divisor>}
%.......................................................................
\newcommand\EuclideanDivisionQuotient[2]{%
  \numexpr(%
    ((#1)/(#2))%
    \ifnum\numexpr(((#1)/(#2))*(#2))>\numexpr(#1)\relax
      \ifnum\numexpr(#2)>0 -\else+\fi 1%
    \fi
  )\relax
}%
\newcommand\EuclideanDivisionRemainder[2]{%
  \numexpr(%
    (#1)-((\EuclideanDivisionQuotient{#1}{#2})*(#2))%
  )\relax
}%
%=======================================================================
\makeatother

\begin{document}

$(0)=(3)\cdot(\the\EuclideanDivisionQuotient{0}{3})+\the\EuclideanDivisionRemainder{0}{3}$\par
$(0)=(-3)\cdot(\the\EuclideanDivisionQuotient{0}{-3})+\the\EuclideanDivisionRemainder{0}{-3}$\par
$(-0)=(3)\cdot(\the\EuclideanDivisionQuotient{-0}{3})+\the\EuclideanDivisionRemainder{-0}{3}$\par
$(-0)=(-3)\cdot(\the\EuclideanDivisionQuotient{-0}{-3})+\the\EuclideanDivisionRemainder{-0}{-3}$\par

$(4)=(3)\cdot(\the\EuclideanDivisionQuotient{4}{3})+\the\EuclideanDivisionRemainder{4}{3}$\par
$(4)=(-3)\cdot(\the\EuclideanDivisionQuotient{4}{-3})+\the\EuclideanDivisionRemainder{4}{-3}$\par
$(-4)=(3)\cdot(\the\EuclideanDivisionQuotient{-4}{3})+\the\EuclideanDivisionRemainder{-4}{3}$\par
$(-4)=(-3)\cdot(\the\EuclideanDivisionQuotient{-4}{-3})+\the\EuclideanDivisionRemainder{-4}{-3}$\par

$(5)=(3)\cdot(\the\EuclideanDivisionQuotient{5}{3})+\the\EuclideanDivisionRemainder{5}{3}$\par
$(5)=(-3)\cdot(\the\EuclideanDivisionQuotient{5}{-3})+\the\EuclideanDivisionRemainder{5}{-3}$\par
$(-5)=(3)\cdot(\the\EuclideanDivisionQuotient{-5}{3})+\the\EuclideanDivisionRemainder{-5}{3}$\par
$(-5)=(-3)\cdot(\the\EuclideanDivisionQuotient{-5}{-3})+\the\EuclideanDivisionRemainder{-5}{-3}$\par

$(6)=(3)\cdot(\the\EuclideanDivisionQuotient{6}{3})+\the\EuclideanDivisionRemainder{6}{3}$\par
$(6)=(-3)\cdot(\the\EuclideanDivisionQuotient{6}{-3})+\the\EuclideanDivisionRemainder{6}{-3}$\par
$(-6)=(3)\cdot(\the\EuclideanDivisionQuotient{-6}{3})+\the\EuclideanDivisionRemainder{-6}{3}$\par
$(-6)=(-3)\cdot(\the\EuclideanDivisionQuotient{-6}{-3})+\the\EuclideanDivisionRemainder{-6}{-3}$\par

\bigskip\hrule\bigskip

\AtLeastKdigits{0}{-2}\par
\AtLeastKdigits{0}{-16}\par
\AtLeastKdigits{0}{-128}\par
\AtLeastKdigits{0}{-1024}\par
\AtLeastKdigits{0}{-16384}\par
\AtLeastKdigits{0}{-131072}\par

\bigskip\hrule\bigskip

\AtLeastKdigits{0}{2}\par
\AtLeastKdigits{0}{16}\par
\AtLeastKdigits{0}{128}\par
\AtLeastKdigits{0}{1024}\par
\AtLeastKdigits{0}{16384}\par
\AtLeastKdigits{0}{131072}\par

\bigskip\hrule\bigskip

\AtLeastKdigits{4}{-2}\par
\AtLeastKdigits{4}{-16}\par
\AtLeastKdigits{4}{-128}\par
\AtLeastKdigits{4}{-1024}\par
\AtLeastKdigits{4}{-16384}\par
\AtLeastKdigits{9}{-131072}\par

\bigskip\hrule\bigskip

\AtLeastKdigits{4}{2}\par
\AtLeastKdigits{4}{16}\par
\AtLeastKdigits{4}{128}\par
\AtLeastKdigits{4}{1024}\par
\AtLeastKdigits{4}{16384}\par
\AtLeastKdigits{9}{131072}\par

\bigskip\hrule\bigskip

The TeX-parameter \verb|\time| was initialized at the start of the \LaTeX-run to the value $\the\time$ while
\hbox{%
  $\the\time=60\cdot\the\EuclideanDivisionQuotient{\time}{60}+\the\EuclideanDivisionRemainder{\time}{60}$%
}.

The time with leading zeros is:
\AtLeastKdigits{2}{\EuclideanDivisionQuotient{\time}{60}}%
:%
\AtLeastKdigits{2}{\EuclideanDivisionRemainder{\time}{60}}%
.

%\time=1439
\time=1401
%\time=1391
%\time=1387
%\time=1380
\loop
  \bigskip\hrule\bigskip

  The TeX-parameter \texttt{\string\time} is initialized to the value $\the\time$\\
  while
  \hbox{%
    $\the\time=60\cdot\the\EuclideanDivisionQuotient{\time}{60}+\the\EuclideanDivisionRemainder{\time}{60}$%
  }.

  The time with leading zeros is:
  \AtLeastKdigits{2}{\EuclideanDivisionQuotient{\time}{60}}%
  :%
  \AtLeastKdigits{2}{\EuclideanDivisionRemainder{\time}{60}}%
  .
  \global\advance\time-60
\ifnum\the\time>-1
\repeat

\end{document}
0

This question is very confusing to me. As far as I know, you have American time (am, pm), European time (17:00) and military time (1700). So this means to answer your question in the title: you just need to remove a colon. The value given by \the\time is not military time, it's just the time in minutes since midnight.

To recreate this more simply (if that is what you want?), you can use

\usepackage{datetime2}

\DTMnewtimestyle{customtimestyle}{%
  \renewcommand{\DTMdisplaytime}[3]{
    #1:#2
  }
}

\DTMsettimestyle{customtimestyle}

\begin{document}

time: \DTMcurrenttime

\end{document}

In the \renewcommand block, the arguments #1, #2 and #3 give the hours, minutes and seconds respectively. So go wild!

output:

enter image description here

You must log in to answer this question.

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