16

Is there a macro that calculates the year and months from a given start date and the current date.

So let's say I have 05/02/2010 (the start date), then it should do the following: Today - 05/02/2010 => 1 year, 1 month. It guess you could add the days as well, but I don't really need that at the moment.

I'm using xelatex if that helps at all.

  • Do you need to apply it in a large scale or do you just need a couple of values? In the latter case, I rather suggest utilising an external tool such as Wolfram Alpha. – Harold Cavendish Mar 29 '11 at 19:02
10

The datenumber package allows you to calculate date differences and return the difference as number of days. You still need to make years and month out of it.

Starting of the example \daydifftoday in the manual:

\documentclass{article}

\usepackage{datenumber}

\newcounter{dateone}
\newcounter{datetwo}

\newcommand{\difftoday}[3]{%
      \setmydatenumber{dateone}{\the\year}{\the\month}{\the\day}%
      \setmydatenumber{datetwo}{#1}{#2}{#3}%
      \addtocounter{datetwo}{-\thedateone}%
      \the\numexpr-\thedatetwo/365\relax\space year(s),
      \the\numexpr(-\thedatetwo - (-\thedatetwo/365)*365)/30\relax\space month(s)
} 
\begin{document}

\difftoday{2010}{02}{01}

\end{document}

This code could be further refined to check if the difference is less than a year, one year or more than one year to display "year(s)" accordantly. The same counts for the month:

\documentclass{article}

\usepackage{datenumber}
\usepackage{calc}

\newcounter{datetoday}
\newcounter{diffyears}
\newcounter{diffmonths}
\newcounter{diffdays}


\newcommand{\difftoday}[3]{%
      \setmydatenumber{datetoday}{\the\year}{\the\month}{\the\day}%
      \setmydatenumber{diffdays}{#1}{#2}{#3}%
      \addtocounter{diffdays}{-\thedatetoday}%
      \ifnum\value{diffdays}>0
        \def\diffbefore{in }%
        \def\diffafter{}%
      \else
        \def\diffbefore{}%
        \def\diffafter{ago}%
        \setcounter{diffdays}{-\value{diffdays}}%
      \fi
      \setcounter{diffyears}{\value{diffdays}/365}%
      \setcounter{diffdays}{\value{diffdays}-365*\value{diffyears}}%
      \setcounter{diffmonths}{\value{diffdays}/30}%
      \setcounter{diffdays}{\value{diffdays}-30*\value{diffmonths}}%
      %
      \diffbefore
      \ifnum\value{diffyears}=0
      \else
        \ifnum\value{diffyears}>1
            \thediffyears\space years,
        \else
            \thediffyears\space year,
        \fi
      \fi
      \ifnum\value{diffmonths}=0
      \else
        \ifnum\value{diffmonths}>1
            \thediffmonths\space months
        \else
            \thediffmonths\space month
        \fi
      \fi
      \diffafter
}

\begin{document}

\difftoday{2011}{02}{01}

\difftoday{2010}{02}{01}

\difftoday{2009}{02}{01}

\difftoday{2009}{01}{01}


\difftoday{2012}{02}{01}

\difftoday{2012}{12}{01}

\end{document}

This gives:

1 month ago
1 year, 1 month ago
2 years, 1 month ago
2 years, 2 months ago
in 10 months
in 1 year, 8 months

This should be fine when the accuracy is months (which are taken as 30 days each). Exact days would require to take the different number of days per month and leap years into account.

Note the \diffbefore and \diffafter macros. You can adjust them to your liking and/or language.

  • Now what you want to do is have this command fetch the language set by babel and use the right words for that language! – Seamus Apr 1 '11 at 13:38
  • @Martin Scharrer, I am using this (thank you, it's great). However, it leaves a space after the month(s) bit which I don't seem to be able to get rid of. Are you able to help? – PercyF2519 Jul 4 '17 at 21:25
  • @PercyF2519: Such spaces are usually generated by the line breaks in the macro code. TeX turns all white spaces into normal spaces. That is why I added % on most lines to comment the line break out. Go through your code and look for lines ending with } or similar. Add a % direct after the }. – Martin Scharrer Jul 19 '17 at 9:00
  • @MartinScharrer, that's worked perfectly, thank you. – PercyF2519 Jul 19 '17 at 12:30
  • I'm seeing a case using the first example where the years calculation is rounding up, so I see "10 years -2 months". – cmbuckley Feb 27 at 14:56

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.