# Compute the difference between two time points on different dates

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:

\documentclass{article}

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

% duration
\def\duration#1#2{%
{\def\,{ }%
\edef\tmp{%
\noexpand\theminutes{%
\noexpand\the
\noexpand\numexpr
(\noexpand\xduration#2\relax)-%
(\noexpand\xduration#1\relax)\relax}}\tmp}}

\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
}

\def\theminutes#1{%
#1\ minute\ifnum#1=1 \else s\fi}

% environment
\newenvironment{tbl}[3]{
\begin{tabular}{ll}
#1 & \duration{#2}{#3}\\
\end{tabular}
}{}

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

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

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

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

\begin{tbl}{Duration}{11:30\,am}{11:31\,am}
\end{tbl}

\end{document}


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.)

Update

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?

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.

\documentclass{article}
\usepackage{datenumber}
\gdef\mornaft{}

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

\newcommand\ampm[1][T]{%
\if T#1%
\def\numfoo##1##2{%
\the\numexpr(\expandafter\xfoo##2\mornaft\relax)-%
(\expandafter\xfoo##1\mornaft\relax)\relax}%
\else%
\def\numfoo##1##2{%
\the\numexpr(\expandafter\xfoo##2 am\relax)-%
(\expandafter\xfoo##1 am\relax)\relax}%
\fi
}
\ampm

\def\barr#1#2{%
\edef\tmp{\the\numexpr((\numfoo{#1}{#2})-(30))/60\relax}%
\edef\tmp{\the\numexpr\tmp+24\relax}\fi%
\ifnum\themydays=0\relax\else
\themydays\ day\ifnum\themydays=1\else s\fi,\ \fi%
\tmp\ hour\ifnum\tmp=1\relax\else s\fi\ and\ %
\edef\tmp{\the\numexpr(\numfoo{#1}{#2})-((\numfoo{#1}{#2})-(30))/60*60}%
\tmp\ minute\ifnum\tmp=1\relax\else s\fi%
}

\def\barm#1#2{%
\edef\tmp{\the\numexpr(\value{mydays})*60*24+\numfoo{#1}{#2}}%
\tmp\ minute\ifnum\tmp=1\else s\fi%
}

\newcounter{mystartdate}
\newcounter{mydays}

\newcommand\timediff[9][d]{%
\setdatenumber{#2}{#3}{#4}%
\setcounter{mystartdate}{\value{datenumber}}%
\setdatenumber{#6}{#7}{#8}%
\setcounter{mydays}{\numexpr\value{datenumber}-\value{mystartdate}}%
\if#1m\barm{#5}{#9}\else\barr{#5}{#9}\fi%
}

\begin{document}
\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}

\ampm[F]
\timediff{2014}{10}{12}{11:30}{2014}{10}{12}{23:31}

\timediff[m]{2014}{10}{12}{11:30}{2014}{10}{12}{23:31}
\end{document}


• Really nice! I have a few more requests, which I'll add to my question. – Svend Tveskæg Nov 15 '14 at 22:49
• @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
• @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

\documentclass{article}

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

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

\end{document}

• 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
• @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