TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this code


and want to compare these times if the time is before or after 18 o clock.

share|improve this question
up vote 9 down vote accepted

With datetime you can print times; if you want an expandable test for times here's a possibility:




  % #1=hour, #2=minutes, #3=seconds
  % #4=code if time is before current time
  % #5=code if time is equal to current time
  % #6=code if time is after current time
    #5% case 0
    #6% case 1
    #4% case -1

  \expandafter\def\expandafter\tim@currenttime@aux\string D:#1#2#3#4#5#6#7#8#9\@nil{%
    \number\numexpr #1#2*3600+#3#4*60+#5#6\relax






The code is independent of datetime and it works with all engines; however, with XeLaTeX, seconds are not available so times that differ only for the seconds part are considered to be equal.

The example was compiled at the shown time (which is the only reason for loading datetime).

enter image description here

share|improve this answer
Thank you very much. That works perfect :) – Tim Aug 15 '14 at 13:42

The seconds are irrelevant because TeX knows only minutes from midnight when it is executed, no seconds. This information is stored in \time primitive register. I created the \giventime macro which is similar to mentioned \formattime, but the third parameter (seconds) is simply ignored.

\def\giventime#1#2#3{\minutes=#1 \multiply\minutes by60 \advance\minutes by#2 }


\ifnum\time<\minutes Current time is before given \fi
\ifnum\time=\minutes Current time is in the same minute as given \fi
\ifnum\time>\minutes Current time is after given \fi

Note, there is no @ in my macros :-)

Edit My solution shows that if we know what is desired and we know TeX primitives then the solution could be much more simple and straghtforward than using LaTeX packages. Using datetime, you must compare two strings and the solution includes complicated macros like in the accepted answer. Using TeX primitives you are comparing simply two numbers.

share|improve this answer
Thank you, your answer is much appreciated, too :) – Tim Aug 15 '14 at 19:22
@your edit: Yes that's true. I would have accepted your answer but there was already a working one. Btw: you could improve your code by checking one if or in worst case two if. Your does always check three if. Here what I mean: \ifnum\time<\minutes Current time is before given \else \ifnum\time=\minutes Current time is in the same minute as given \else Current time is after given \fi \fi – Tim Aug 16 '14 at 12:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.