This is a follow-up question to How to compute the difference between two time points (e.g., 11:30 am and 01:20 pm => 110 min)?.

Consider the code from David's excellent answer here:


\newcommand*{\mystart}{11:30 am}
\newcommand*{\myend}{01:20 pm}

% duration
  {\def\,{ }%

\def\xduration#1:#2 #3m#4\relax{%
%(#1)*60+#2\if p#3+720\fi
 (#1)*60+#2\if p#3\ifnum#1=12 \else+720\fi\fi

#1\ minute\ifnum#1=1 \else s\fi}

% environment
    #1 & \duration{#2}{#3}\\

\begin{tbl}{Duration}{11:30 am}{01:20 pm}% works

\begin{tbl}{Duration}{\mystart}{\myend}% Runaway argument? ! File ended while scanning use of \xduration.

\begin{tbl}{Duration}{11:30\,am}{01:20\,pm}% works

\begin{tbl}{Duration}{12:30 pm}{03:00 pm} % works



Can the macro be expanded to also deal with time differences between differnate dates? (For example compute the number of minutes from November 15th, 17:20 pm, to November 18th, 09:00 am.)


Following the nice answer of Steven B. Segletes, I would like to omit <a> day(s), <b> hour(s), and/or <c> minute(s) if the value(s) is/are zero.

Example 1: I would like \timediff{2014}{10}{12}{11:30 am}{2014}{10}{12}{01:20 pm} to produce 1 hour and 50 minutes.

Example 2: I would like \timediff{2014}{10}{12}{11:30 am}{2014}{10}{14}{08:50 am} to produce 1 day, 21 hours and 20 minutes. [I can implement the additional , and and myself, I assume.]

Update 2

I forgot two more thing:

  • Can I get the output as <a> days, <b> hours and <c> minutes = <d> minutes, i.e., also get the output in minutes?
  • Is it possible to input the time of the day as <p>:<q> where <p> is an integer between 0 and 24 and <q> is an integer between 0 and 59, instead of <p>:<q> am/<p>:<q> pm?

(I'll accept your answer as soon as the last thing is added.)


Using the datenumber package to do day arithmetic, and using my prior answer from the OP's cited question as the basis for my hr:mm arithmetic. The datenumber package allows the conversion of a date into a counter number (relative to a reference date).

Syntax is:

\timediff{year1}{month1}{day1}{h1:m1 am/pm}{year2}{month2}{day2}{h2:m2 am/pm}

EDITED to provide [m] optional argument allowing output solely in minutes. Also provide \ampm[T/F] to set the hours minutes as {1:31 pm} (T) or as {13:31} (F).

I should point out there is no error checking for the valid range of inputs.


\def\xfoo#1:#2 #3m#4\relax{(#1)*60+#2\if p#3+720\fi\ifnum#1=12-720\fi}

\if T#1%
  \the\numexpr(\expandafter\xfoo##2 am\relax)-%
    (\expandafter\xfoo##1 am\relax)\relax}%

    \themydays\ day\ifnum\themydays=1\else s\fi,\ \fi%
  \tmp\ hour\ifnum\tmp=1\relax\else s\fi\ and\ %
  \tmp\ minute\ifnum\tmp=1\relax\else s\fi%

  \tmp\ minute\ifnum\tmp=1\else s\fi%



\timediff{2014}{10}{12}{11:30 am}{2014}{10}{12}{01:20 pm}

\timediff{2014}{10}{12}{11:30 am}{2014}{10}{14}{08:50 am}

\timediff{2014}{10}{12}{11:30 am}{2014}{11}{14}{07:00 am}

\timediff{2014}{10}{12}{11:30 am}{2014}{11}{14}{12:31 pm}=
\timediff[m]{2014}{10}{12}{11:30 am}{2014}{11}{14}{12:31 pm}

\timediff[m]{2014}{10}{12}{11:30 am}{2014}{10}{12}{11:31 am}



enter image description here

  • Really nice! I have a few more requests, which I'll add to my question. – Svend Tveskæg Nov 15 '14 at 22:49
  • 1
    @SvendTveskæg Please see revision. – Steven B. Segletes Nov 15 '14 at 23:35
  • I forgot two more things (sorry); I've updated my question again. – Svend Tveskæg Nov 16 '14 at 0:20
  • 1
    @SvendTveskæg Please see re-revision. – Steven B. Segletes Nov 16 '14 at 2:02

You could spend an hour or two writing this in TeX, or just use --shell-escape and

enter image description here


\edef\pc{\csname @percentchar\endcsname}
time between #1 and #2 is
\input{"|echo '(' ` date  --date '#1' +\pc s`  - `date  --date '#2' +\pc s` ')/60' | bc"}%

\foo{9am November 19 2014}{5pm November 15 2014}

  • Nice. Is it possible to get From <time, date, year> to <time, date, year> there are <n> minutes [with commas between time, date, and year] and a space between, say, 5 and pm? (Btw., if you have an hour or two and don't know what to do with your time, you shall be more than welcome to write it in TeX.) – Svend Tveskæg Nov 15 '14 at 17:48
  • @SvendTveskæg date is very flexible in what it accepts now for example:-) but exactly what it accepts depends on the version you have, the locale and other things, try date --help on the commandline. otherwise you can add sed to the pipe to remove commas and reorder fields – David Carlisle Nov 15 '14 at 18:15
  • I'll try and fiddle with it; thanks. Just out of curiosity: Do yo think that you'll have time and energy to write it using TeX? :) – Svend Tveskæg Nov 15 '14 at 18:21
  • 1
    @SvendTveskæg after I've done longtable v5, that's running at a rate of about 1 release every 10 years... – David Carlisle Nov 15 '14 at 18:23
  • Okay. I just assumed that with your TeX skills it wouldn't take a very long time. :) – Svend Tveskæg Nov 15 '14 at 18:33

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.