# [Tex/LaTex] How to compute the difference between two time points (e.g., 11:30 am and 01:20 pm => 110 min)

calculationsdatetime

Is there a simple way to compute the difference between two times (same day), so 11:30 am and 01:20 pm should lead to 110 min (or 1h 50min), for example? I saw the datenumber package but it only seems to compute day differences (so works on a larger scale). [I didn't include a minimal example as I don't have any elegant way how to start the problem except for (ugly?) string 'divide-and-conquer' approaches]

Okay, I just also discovered datetime which looks more promising. Nevertheless, I don't see how one can compute differences of time points with this package (or calc?)

Update
After David's answer, I expanded the MWE to my original example and realized the following Runaway argument-problem:

\documentclass{article}

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

% duration
\def\duration#1#2{%
\the\numexpr(\xduration#2\relax)-(\xduration#1\relax)\relax\ minutes}

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

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

\begin{document}
\begin{tbl}{Duration}{\mystart}{\myend}% Runaway argument? ! File ended while scanning use of \xduration.
%\begin{tbl}{Duration}{11:30 am}{01:20 pm}% works
\end{tbl}
\end{document}


How can this be avoided? [another pair of {} did not help]

\documentclass{article}

\def\foo#1#2{%
\the\numexpr(\xfoo#2\relax)-(\xfoo#1\relax)\relax\ minutes}

\def\xfoo#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
}

\begin{document}

\foo{11:30 am}{01:20 pm}

\end{document}


If you want to expand the arguments before parsing, as in the edited MWE:

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